數(shù)據(jù)庫查詢構(gòu)造器 (query builder) 提供方便、流暢的接口,用來建立及執(zhí)行數(shù)據(jù)庫查找語法。在你的應(yīng)用程序里面,它可以被使用在大部分的數(shù)據(jù)庫操作,而且它在所有支持的數(shù)據(jù)庫系統(tǒng)上都可以執(zhí)行。
注意: Laravel 查詢構(gòu)造器使用 PDO 參數(shù)綁定,以保護(hù)應(yīng)用程序免于 SQL 注入,因此傳入的參數(shù)不需額外轉(zhuǎn)義特殊字符。
$users = DB::table('users')->get();
foreach ($users as $user)
{
var_dump($user->name);
}
DB::table('users')->chunk(100, function($users)
{
foreach ($users as $user)
{
//
}
});
通過在 閉包
中返回 false
來停止處理接下來的數(shù)據(jù)列:
DB::table('users')->chunk(100, function($users)
{
//
return false;
});
$user = DB::table('users')->where('name', 'John')->first();
var_dump($user->name);
$name = DB::table('users')->where('name', 'John')->pluck('name');
$roles = DB::table('roles')->lists('title');
這個(gè)方法將會(huì)返回?cái)?shù)據(jù)表 role 的 title 字段值的數(shù)組。你也可以通過下面的方法,為返回的數(shù)組指定自定義鍵值。
$roles = DB::table('roles')->lists('title', 'name');
$users = DB::table('users')->select('name', 'email')->get();
$users = DB::table('users')->distinct()->get();
$users = DB::table('users')->select('name as user_name')->get();
$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();
### 排序(Order By)、分群(Group By) 及 Having
$users = DB::table('users')
->orderBy('name', 'desc')
->groupBy('count')
->having('count', '>', 100)
->get();
$users = DB::table('users')->skip(10)->take(5)->get();
查詢構(gòu)造器也可以使用 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();
DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->get();
你也可以指定更高級的 join 子句:
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
})
->get();
如果你想在你的 join 中使用 where 型式的子句,你可以在 join 子句里使用 where
或 orWhere
方法。下面的方法將會(huì)比較 contacts 數(shù)據(jù)表中的 user_id 的數(shù)值,而不是比較兩個(gè)字段。
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
有些時(shí)候你需要更高級的 where 子句,如「where exists」或嵌套的群組化參數(shù)。Laravel 的查詢構(gòu)造器也可以處理這樣的情況:
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
上面的查找語法會(huì)產(chǎn)生下方的 SQL:
select * from users where name = 'John' or (votes > 100 and title <> 'Admin')
DB::table('users')
->whereExists(function($query)
{
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();
上面的查找語法會(huì)產(chǎn)生下方的 SQL:
select * from users
where exists (
select 1 from orders where orders.user_id = users.id
)
查找產(chǎn)生器也提供各式各樣的聚合方法,如 count
、max
、min
、avg
及 sum
。
$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');
有些時(shí)候你需要使用原生表達(dá)式在查找語句里,這樣的表達(dá)式會(huì)成為字串插入至查找,因此要小心勿建立任何 SQL 注入點(diǎn)。要建立原生表達(dá)式,你可以使用 DB::raw
方法:
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
DB::table('users')->insert(
array('email' => 'john@example.com', 'votes' => 0)
);
如果數(shù)據(jù)表有自動(dòng)遞增的 ID,可以使用 insertGetId
添加數(shù)據(jù)并返回該 ID:
$id = DB::table('users')->insertGetId(
array('email' => 'john@example.com', 'votes' => 0)
);
注意: 當(dāng)使用 PostgreSQL 時(shí),insertGetId 方法會(huì)預(yù)期自動(dòng)增加的字段是以「id」為命名。
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));
DB::table('users')->increment('votes');
DB::table('users')->increment('votes', 5);
DB::table('users')->decrement('votes');
DB::table('users')->decrement('votes', 5);
也能夠同時(shí)指定其他要更新的字段:
DB::table('users')->increment('votes', 1, array('name' => 'John'));
DB::table('users')->where('votes', '<', 100)->delete();
DB::table('users')->delete();
DB::table('users')->truncate();
查詢構(gòu)造器也提供一個(gè)快速的方法去「合并 (union)」兩個(gè)查找的結(jié)果:
$first = DB::table('users')->whereNull('first_name');
$users = DB::table('users')->whereNull('last_name')->union($first)->get();
unionAll
方法也可以使用,它與 union
方法的使用方式一樣。
查詢構(gòu)造器提供了少數(shù)函數(shù)協(xié)助你在 SELECT 語句中做到「悲觀鎖定」。
想要在 SELECT 語句中加上「Shard lock」,只要在查找語句中使用 sharedLock
函數(shù):
DB::table('users')->where('votes', '>', 100)->sharedLock()->get();
要在 select 語法中使用「鎖住更新(lock for update)」時(shí),你可以使用 lockForUpdate
方法:
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();