我想用 where 條件過濾我的查詢。假設我有一個 Product 模型,它與 Buyer 模型有關系。我想獲得從一組 65 歲及以上的買家那里購買的產品型號列表。$query = Product::with('Buyer')
->where('Buyer.age','>=',65)
->get();但是,我無法放置這樣的 where 條件,它顯示“Buyer.age 的未知列”。請告知如何過濾我的結果?
2 回答
米琪卡哇伊
TA貢獻1998條經驗 獲得超6個贊
試試這個。
$query = Product::with('Buyer')
->whereHas('Buyer', function($q){ // whereHas will only return products whose buyer is >= 65
$q->where('age','>=',65);
})
->get();
動漫人物
TA貢獻1815條經驗 獲得超10個贊
請嘗試一下:
$query = Product::whereHas('Buyer', function ($query) {
$query->where('age', '>=', 65);
})->with(['Buyer' => function($q){
$q->where('age','>=',65);
}])
->get();
- 2 回答
- 0 關注
- 175 瀏覽
添加回答
舉報
0/150
提交
取消
