简体   繁体   English

Laravel Eloquent-关系是否具有关系?

[英]Laravel Eloquent - Does the relation having the relation?

I have a model Alpha which has many model Beta which has many model Charlie I wan't all the beta from one alpha which have at least one charlie. 我有一个Alpha模型,其中有许多Beta模型,其中有许多Charlie模型,我并不想从一个Alpha中至少有一个Charlie的所有Beta中获得所有Beta。

So my guess was : 所以我的猜测是:

$alpha = Alpha::first();
$betasWithACharlie = $alpha->betas()->has('charlies')->get();

But this doesn't work. 但这是行不通的。 I also tried with "whereHas" unsuccessfully. 我也尝试使用“ whereHas”失败。

Simply: 只是:

$betasWithACharlie = $alpha->betas()->has('charlies', '>', 1)->get();

Or you can retrieve 'charlies' only if they fulfill a certain condition for more advanced queries 或者,只有在满足某些条件以进行更高级的查询时,您才能检索“查理”

$betasWithACharlie = $alpha->betas()->whereHas('charlies', function($query){
$query->where('condition');
})->get();

I have a thought that i don't test before: 我认为我之前没有测试过:

$betasWithACharlie = $alpha->betas()->whereHas('charlies', function ($query) {
    $query->where('beta_id', '!=', 0);
})->get();

And try another one: 并尝试另一个:

$betasWithACharlie = Alpha::whereHas('betas', function ($query) {
    $query->has('charlies');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM