【发布时间】:2018-03-05 14:40:23
【问题描述】:
我正在尝试搜索所有具有 RentItem 的 RentItem,其书名与给定的 $input 类似。
问题是当输入不匹配时,我仍然得到一个集合返回。 唯一的区别是 book 关系是 null 而不是集合。
应该返回 false 的查询结果: https://pastebin.com/pd7UqhCi
查询结果为真: https://pastebin.com/shndvdMh
当 book 等于 null 时,我不希望返回 Rent 模型。
我的查询
$rents = Rent::with(['rentItems.book' => function ($query) use ($input) {
$query->where('books.title', 'LIKE', "%$input%");
}])->get();
租模关系
public function rentItems()
{
return $this->hasMany(RentItem::class);
}
RentItems 模型关系
public function book()
{
return $this->belongsTo(Book::class);
}
public function rent()
{
return $this->belongsTo(Rent::class);
}
我做过的研究:
【问题讨论】:
标签: php laravel collections nested-queries