提问者:小点点

laravel雄辩地


我有这个问题,

$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();

这将返回预期的标记,但不返回预期的列表,关系如下所示,

标记列表具有n:n关系列表类型具有n:n关系

我想要实现的是查询所有标签的数据,这些标签的清单的开销为0.00null,并且基于其n:n关系具有1类型。

然而,我得到的是,返回的标记具有清单(由whereHas提供),但清单实际上不是响应的一部分,如果我使用('listings')执行$query->,那么似乎忽略了我的开销where子句,返回了标记中的所有清单

任何想法,我可以如何获得所有标签,有清单和那些清单要通过他们的成本是否为零,如果有类型(n:nrelationship)是1(或任何其他数字)来选择


共2个答案

匿名用户

LaravelMySQL中,您不能做column=null,您必须在MySQL中使用columnis null。 因此Laravel对此也有特殊的条件,而是使用WhereNull()。 因此将成本where改为以下内容。

->orWhereNull('cost')

匿名用户

请尝试此查询

$listings = Tag::whereHas('listings', function($query) use ($request) {
                $query->where('moderated', 1)
                    ->where('active', 1)
                    ->where('cost', '=', '0.00')
                    ->orWhereNull('cost')
                    ->with(['types' => function($q) {
                      $q->whereIn('type_id', [1]);
                    }])
                    ->with('primaryImage');
                })->get();