I have this query,
$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();
This returns the expected tags, however it does not return the expected listings, the relationships looks like this,
tag
and listings
have a n:n
relationship listing
and type
have a n:n
relationship
What I am wanting to achieve is query the data for all tags that have listings that have a cost of 0.00
or NULL
and also have a type of 1 based on its n:n
relationship.
What I am getting however is tags
are being returned that have listings
(courtesy of the whereHas) but the listing isn't actually part of the response, and if I do $query->with('listings')
it seems to disregard my cost where clause and return all listings
in that tag
?
Any ideas of how I can get all tags that have listings and those listings to be selected via whether their cost is zero and if there type ( n:n
relationship) is 1
(or anything other number)
In Laravel
and MySql
you can not do column = null
, you have to use column is null
in MySql
. Therefor Laravel
also has special condition for this, instead use whereNull()
. So change the cost where to the following.
->orWhereNull('cost')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.