6. Laravel 模型增、删、改操作
Laravel 模型增、删、改操作
写
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);
删
$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();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?