W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
如上所示,你可以在查詢關(guān)聯(lián)時自由添加其他約束。但是,在將 orWhere
子句鏈接到關(guān)聯(lián)時要小心,因為 orWhere
子句將在邏輯上與關(guān)聯(lián)約束處于同一級別:
$user->posts()
->where('active', 1)
->orWhere('votes', '>=', 100)
->get();
// select * from posts
// where user_id = ? and active = 1 or votes >= 100
在大多數(shù)情況下,你可以使用 約束組 在括號中對條件檢查進行邏輯分組:
use Illuminate\Database\Eloquent\Builder;
$user->posts()
->where(function (Builder $query) {
return $query->where('active', 1)
->orWhere('votes', '>=', 100);
})
->get();
// select * from posts
// where user_id = ? and (active = 1 or votes >= 100)
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: