DB::table('users')->insert([
'email' => 'kayla@example.com',
'votes' => 0
]);
DB::table('users')->insert([
['email' => 'picard@example.com', 'votes' => 0],
['email' => 'janeway@example.com', 'votes' => 0],
]);
DB::table('users')->insertOrIgnore([
['id' => 1, 'email' => 'sisko@example.com'],
['id' => 2, 'email' => 'archer@example.com'],
]);
DB::table('pruned_users')->insertUsing([
'id', 'name', 'email', 'email_verified_at'
], DB::table('users')->select(
'id', 'name', 'email', 'email_verified_at'
)->where('updated_at', '<=', now()->subMonth()));
$id = DB::table('users')->insertGetId(
['email' => 'john@example.com', 'votes' => 0]
);
$affected = DB::table('users')
->where('id', 1)
->update(['votes' => 1]);
更新数据库中的现有记录,不存在则创建
使用第一个参数的列和值对来定位匹配的数据库记录。 如果记录存在,它将使用第二个参数中的值进行更新。如果找不到记录,将插入一条新记录。
DB::table('users')
->updateOrInsert(
['email' => 'john@example.com', 'name' => 'John'],
['votes' => '2']
);
DB::table('users')->increment('votes');
DB::table('users')->increment('votes', 5);
DB::table('users')->decrement('votes');
DB::table('users')->decrement('votes', 5);
DB::table('users')->increment('votes', 1, ['name' => 'John']);
$deleted = DB::table('users')->delete();
$deleted = DB::table('users')->where('votes', '>', 100)->delete();
DB::table('users')->truncate();