zl程序教程

您现在的位置是:首页 >  其它

当前栏目

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();