查詢
獲取多條數(shù)據(jù)
$users = DB::table('users')->get();
獲取單條數(shù)據(jù)
$user = DB::table('users')->where('name', 'John')->first();
獲取單條數(shù)據(jù)單個列
$name = DB::table('users')->where('name', 'John')->pluck('name');
獲取單個值的多條數(shù)據(jù)
$roles = DB::table('roles')->lists('title');
獲取數(shù)組的kv列表
DB::table('roles')->pluck('title','key');
按字段條件去重
DB::table('roles')->distinct('key');
只返回某個字段的值,以string返回
DB::table('roles')->value('title');
查詢中應用查詢
$query = DB::table('users')->select('name');
$users = $query->addSelect('age')->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', array(1, 100))->get();
$users = DB::table('users')->whereNotBetween('votes', array(1, 100))->get();
$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
$users = DB::table('users')->whereNotIn('id', array(1, 2, 3))->get();
$users = DB::table('users')->whereNull('updated_at')->get();
$users = DB::table('users')->orderBy('name', 'desc')->groupBy('count')->having('count', '>', 100)->get();
$users = DB::table('users')->skip(10)->take(5)->get(); //Offset & Limit
復合查詢
- 基礎的JOIN
DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.id', 'contacts.phone', 'orders.price')
->get();
- left join
DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->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();
- 分組
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')
DB::table('users')
->whereExists(function(query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();
select * from userswhere exists (
select 1 from orders where orders.user_id = users.id
)
聚合查詢
$users = DB::table('users')->count();
$price = DB::table('orders')->max('price');
$price = DB::table('orders')->min('price');
$price = DB::table('orders')->avg('price');
$total = DB::table('users')->sum('votes');
聚合結合復合查詢
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
遞增和遞減
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, array('name' => 'John'));
插入
- 插入
DB::table('users')->insert(
array('email' => 'john@example.com', 'votes' => 0)
);
- 插入后獲取自增ID
$id = DB::table('users')->insertGetId(
array('email' => 'john@example.com', 'votes' => 0)
);
- 多條記錄一起插入
DB::table('users')->insert(array(
array('email' => 'taylor@example.com', 'votes' => 0),
array('email' => 'dayle@example.com', 'votes' => 0),
));
更新
DB::table('users')->where('id', 1)->update(array('votes' => 1));
刪除
- 刪除指定條件的數(shù)據(jù)
DB::table('users')->where('votes', '<', 100)->delete();
- 刪除表里所有數(shù)據(jù)
DB::table('users')->delete();
- 刪除這個表
DB::table('users')->truncate();
聯(lián)盟2個查詢; unionAll方法也可以,有相同的方法簽名。
$first = DB::table('users')->whereNull('first_name');
$users = DB::table('users')->whereNull('last_name')->union($first)->get();
鎖
共享鎖
DB::table('users')->where('votes', '>', 100)->sharedLock()->get();
更新鎖
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
緩存
查詢的結果將為十分鐘被緩存般甲。查詢結果緩存時,不會對數(shù)據(jù)庫運行,結果將從默認的緩存加載驅動程序指定您的應用程序氧吐。
$users = DB::table('users')->remember(10)->get();
如果您使用的是支持緩存的司機,還可以添加標簽來緩存:
$users = DB::table('users')->cacheTags(array('people', 'authors'))->remember(10)->get();