$deleted = DB::delete('delete from users');
$affected = DB::update('update users set votes = 100 where name = ?', ['John']);
$users = DB::select('select * from users where active = ?', [1]);
foreach ($users as $user) { echo $user->name; }
DB::transaction(function () { DB::table('users')->update(['votes' => 1]); DB::table('posts')->delete(); });
DB::beginTransaction(); if($someContion){ DB::rollback(); return false; } DB::commit();
DB::statement('drop table users');
$users = DB::table('users')->get();
foreach ($users as $user) { echo $user->name; //操作的是对象的属性 }
$user = DB::table('users')->where('name', 'John')->first();
$email = DB::table('users')->where('name', 'John')->value('email');
DB::table('users')->chunk(100, function($users) { foreach ($users as $user) { // } });
DB::table('users')->chunk(100, function($users) { // 处理记录… return false; });
$titles = DB::table('roles')->pluck('title');
foreach ($titles as $title) { echo $title; }
$roles = DB::table('roles')->pluck('title', 'name'); foreach ($roles as $name => $title) { echo $title; }
$users = DB::table('users')->select('name', 'email as user_email')->get();
$users = DB::table('users')->distinct()->get();
$query = DB::table('users')->select('name'); $users = $query->addSelect('age')->get();
$users = DB::table('users') ->join('contacts', 'users.id', '=', 'contacts.user_id') ->join('orders', 'users.id', '=', 'orders.user_id') ->select('users.*', 'contacts.phone', 'orders.price') ->get();
DB::table('users') ->join('contacts', function ($join) { $join->on('users.id', '=', 'contacts.user_id')->orOn(...); }) ->get();
DB::table('users') ->join('contacts', function ($join) { $join->on('users.id', '=', 'contacts.user_id') ->where('contacts.user_id', '>', 5); }) ->get();
$users = DB::table('users')->where('votes', '=', 100)->get(); $users = DB::table('users')->where('name', 'like', 'T%')->get(); $users = DB::table('users')->where('votes', 100)->get(); //省略了等号 $users = DB::table('users')->where('votes', '>', 100) ->orWhere('name', 'John')->get(); $users = DB::table('users')->whereBetween('votes', [1, 100])->get(); //一个字段的值介于两个值之间 $users = DB::table('users')->whereNotBetween('votes', [1, 100])->get(); //一个字段的值落在两个值之外 $users = DB::table('users')->whereIn('id', [1, 2, 3])->get(); //字段的值包含在指定的数组之内 $users = DB::table('users')->whereNotIn('id', [1, 2, 3])->get(); //字段的值不包含在指定的数组之内 $users = DB::table('users')->whereNull('updated_at')->get(); //指定列的值为 NULL $users = DB::table('users')->whereNotNull('updated_at')->get(); //一个列的值不为 NULL
DB::table('users') ->where('name', '=', 'John') ->orWhere(function ($query) { $query->where('votes', '>', 100) ->where('title', '<>', 'Admin'); }) ->get();
select * from users where name = 'John' or (votes > 100 and title <> 'Admin')
$users = DB::table('users') ->orderBy('name', 'desc') ->get();
$randomUser = DB::table('users') ->inRandomOrder() ->first();
$users = DB::table('users') ->groupBy('account_id') ->having('account_id', '>', 100) ->get();
$users = DB::table('orders') ->select('department', DB::raw('SUM(price) as total_sales')) ->groupBy('department') ->havingRaw('SUM(price) > 2500') ->get();
$role = $request->input('role'); $users = DB::table('users') ->when($role, function ($query) use ($role) { return $query->where('role_id', $role); }) ->get();
DB::table('users')->insert( ['email' => '[email protected]', 'votes' => 0] );
DB::table('users')->insert([ ['email' => '[email protected]', 'votes' => 0], ['email' => '[email protected]', 'votes' => 0] ]);
$id = DB::table('users')->insertGetId( ['email' => '[email protected]', 'votes' => 0] );
DB::table('users') ->where('id', 1) ->update(['votes' => 1]);
DB::table('users')->delete(); DB::table('users')->where('votes', '<', 100)->delete(); //若你希望截去整个数据表的所有数据列,并将自动递增 ID 重设为零,则可以使用 truncate 方法: DB::table('users')->truncate();
DB::table('users')->where('votes', '>', 100)->sharedLock()->get();
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();