简体   繁体   English

Laravel雄辩得到特定的关系

[英]Laravel Eloquent get specific relationship

I'm trying to get the books where the authors are from a specific country, for now my query looks like this : 我正在尝试从特定国家/地区获取作者所在的图书,目前,我的查询如下所示:

$books->with('author')->where('country', '=',  'FR'); 

but the where is applied to books and not to authors 但在哪里适用于书籍而不是作者

any help ? 有什么帮助吗?

You need to use whereHas() for that. 您需要为此使用whereHas() The following code should do the trick: 以下代码可以解决问题:

$country = 'FR';
$books = Book::with('author')->whereHas('author', function($query) use ($country) {
  $query->where('country', '=',  $country);
})->get();

This will give you all books that have a related author, that has country column set to FR . 这将为您提供所有具有相关作者的书,并且“ 国家/地区”列设置为FR

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

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