[英]Laravel Eloquent - Does the relation having the relation?
我有一個Alpha模型,其中有許多Beta模型,其中有許多Charlie模型,我並不想從一個Alpha中至少有一個Charlie的所有Beta中獲得所有Beta。
所以我的猜測是:
$alpha = Alpha::first();
$betasWithACharlie = $alpha->betas()->has('charlies')->get();
但這是行不通的。 我也嘗試使用“ whereHas”失敗。
只是:
$betasWithACharlie = $alpha->betas()->has('charlies', '>', 1)->get();
或者,只有在滿足某些條件以進行更高級的查詢時,您才能檢索“查理”
$betasWithACharlie = $alpha->betas()->whereHas('charlies', function($query){
$query->where('condition');
})->get();
我認為我之前沒有測試過:
$betasWithACharlie = $alpha->betas()->whereHas('charlies', function ($query) {
$query->where('beta_id', '!=', 0);
})->get();
並嘗試另一個:
$betasWithACharlie = Alpha::whereHas('betas', function ($query) {
$query->has('charlies');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.