Laravel 模型增、删、改操作
操作 模型 laravel
2023-09-11 14:14:57 时间
写
App\User::insert(
['email' => 'john@example.com', 'votes' => 0]
);
\App\User::insert([
['email' => 'taylor@example.com', 'votes' => 0],
['email' => 'dayle@example.com', 'votes' => 0]
]);
\App\User::insertOrIgnore([
['id' => 1, 'email' => 'taylor@example.com'],
['id' => 2, 'email' => 'dayle@example.com']
]);
$id = \App\User::insertGetId(
['email' => 'john@example.com', 'votes' => 0]
);
# PostgreSQL 的 insertGetId 默认自增字段是 id,如果是其他的,需要传入字段名到 insertGetId 第二个参数。
$flight = new Flight;
$flight->name = $request->name;
$flight->save();
改
$numbersOfRowsAffected = \App\User::where('id', 1)->update(['votes' => 1]);
// 当通过模型批量更新时,saving, saved, updating, and updated 模型事件将不会被更新后的模型触发。这是因为批量更新时,模型从来没有被取回。
$flight = App\Flight::find(1);
$flight->name = 'New Flight Name';
$flight->save();
# json
\App\User::where('id', 1)->update(['options->enabled' => true]);
App\User::increment('votes');
\App\User::increment('votes', 5);
\App\User::increment('votes', 1, ['name' => 'John']);
\App\User::decrement('votes');
\App\User::decrement('votes', 5);
【laravel】updateOrCreate 和 updateOrInsert 的区别
updateOrCreate()
和 updateOrInsert()
两个方法都是用来保存数据的时候方便操作“ 存在即更新,反之则创建 ”的updateOrCreate
方法使用的是 Eloquent ORM 操作的数据库(支持自动添加创建和更新时间),updateOrInsert
方法使用的是查询构造器(不可以自动添加创建和更新时间)updateOrCreate
返回值是\Illuminate\Database\Eloquent\Model, updateOrInsert
返回的是 bool
。可以看两个方法的源码注释部分的 @return下面是updateOrCreate
文档说明和源码。
//存在则修改数据,不存在则添加
$where['m_product_id'] = $v['productId'];
$where['valid'] = 1;
$create_bool=Product::updateOrCreate($where,$data);
删
$numbersOfRowsAffected = \App\User::delete();
$numbersOfRowsAffected = \App\User::where('votes', '>', 100)->delete();
\App\User::truncate();
$flight = App\Flight::find(1); // 取回模型再删除
$flight->delete();
// 或者
App\Flight::destroy(1); // 直接删除
App\Flight::destroy([1, 2, 3]);
App\Flight::destroy(1, 2, 3);
App\Flight::destroy(collect([1, 2, 3]));
当使用 Eloquent 批量删除语句时,`deleting` 和 `deleted` 模型事件不会在被删除模型实例上触发。因为删除语句执行时,不会检索模型实例。
软删除
use SoftDeletes;
protected $dates = ['deleted_at'];
启用软删除的模型时,被软删除的模型将会自动从所有查询结果中排除。
要确认指定的模型实例是否已经被软删除
if ($flight->trashed()) {
//
}
查询包含被软删除的模型
$flights = App\Flight::withTrashed()
->where('account_id', 1)
->get();
只取出软删除数据
$flights = App\Flight::onlyTrashed()
->where('airline_id', 1)
->get();
恢复软删除的模型
$flight->restore();
App\Flight::withTrashed()
->where('airline_id', 1)
->restore();
永久删除模型
// 强制删除单个模型实例...
$flight->forceDelete();
相关文章
- 一个由正则表达式引发的血案 vs2017使用rdlc实现批量打印 vs2017使用rdlc [asp.net core 源码分析] 01 - Session SignalR sql for xml path用法 MemCahe C# 操作Excel图形——绘制、读取、隐藏、删除图形 IOC,DIP,DI,IoC容器
- laravel操作mysql中JSON类型的字段
- PHP使用组件构建自己的PHP框架【使用 Eloquent 模型进行数据库操作】
- 《面向对象的思考过程(原书第4版)》一3.6 对象操作
- Jquery - Select 和 Checkbox 、Textarea的操作
- 【python】+操作csv文件
- java代码运行linux shell操作
- ThinkPHP 学习笔记 ( 三 ) 数据库操作之数据表模型和基础模型 ( Model )
- Mac Navicat12.0.22 安装手册和操作说明
- 《Access 2007开发指南(修订版)》一一2.5 字段属性操作
- MapReduce案例—分别通过Reduce端和Map端实现JOIN操作
- java 操作流的步骤
- iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义
- Oracle11G在linux环境下的卸载操作
- 【Python小技巧】sqlalchemy操作sqlite日期类型的处理
- Operation not allowed after ResultSet closed--操作mysql数据库
- 人人都能做的机器学习——autokeras项目,3行代码就能训练一个模型,傻瓜式操作,分分钟学会训练模型