I want to get all rows, that is opposite of belongsToMany
relationship on Laravel.
For example, there is events
table and visits
table. If a user has visited an event, then visited on pivot event_visit
table is marked 1.
I want to get all those events, that are not visited.
For example, User model:
public function userVisitedEvents()
{
return $this->belongsToMany(Visits::class, 'event_visit')
->where('visited', '=', '1');
}//end userVisitedEvents()
This selects all visited events.
What I want to achieve: I need to get the opposite, get all events, that are either:
- marked as visited 0
- this visit is not stored at all (there are no relation)
The problem is there because belongsToMany
relation is created using INNER JOIN
. If it would be created with LEFT JOIN
- no problem.
PS - ->where('visited', '<>', '1');
won't work, because it would find only (1) case, not all events.
Maybe there is a chance to tell which join to use? Or the only option would be to use query builder by hand?