我有这个查询,
$listings = Tag::whereHas('listings', function($query) use ($request) {
$query->where('moderated', 1)
->where('active', 1)
->where('cost', '=', '0.00')
->orWhere('cost', '=', NULL)
->with(['types' => function($q) {
$q->whereIn('type_id', [1]);
}])
->with('primaryImage');
})->get();
这将返回预期的标签,但是不返回预期的列表,关系如下所示,
tag
和listings
有n:n
关系
listing
和type
有n:n
关系
我要实现的是查询所有标签的数据,这些标签包含成本为0.00
或NULL
且基于其n:n
的类型为1的列表。关系。
但是我得到的是返回tags
并返回listings
(由whereHas提供),但列表实际上不是响应的一部分,如果我这样做了,{{1} }似乎无视我的cost where子句,并返回$query->with('listings')
中的所有listings
吗?
关于如何获取所有具有列表的标签以及如何通过其成本是否为零以及类型(tag
关系为n:n
(或其他任何数字)来选择那些列表的任何想法)
答案 0 :(得分:0)
在Laravel
和MySql
中,您不能执行column = null
,而必须在column is null
中使用MySql
。因此,Laravel
对此也有特殊条件,请改用whereNull()
。因此,将成本更改为以下位置。
->orWhereNull('cost')