0

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:

  1. marked as visited 0
  2. 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?

Rozkalns
  • 510
  • 2
  • 6
  • 26
  • It's not a concern of the user to fetch all non visited events. You need to provide that using a query scope from the Visit model – ExoticSeagull Feb 22 '17 at 18:26
  • A solution that might not be pretty would be to do that select, and save the id's , then do another one doing the oposite,( getting event from visit) – Sérgio Reis Feb 22 '17 at 18:28

1 Answers1

2

You are looking for "Relationship Absence"

$events = Event::doesntHave('userVisitedEvents')->get();

You may have to adapt it to your context. If you can't work with it, also check

Event::whereDoesntHave(...); //takes a closure.

Source: https://laravel.com/docs/5.4/eloquent-relationships#querying-relationship-absence

EddyTheDove
  • 12,979
  • 2
  • 37
  • 45
  • Seems like I've missed this in documentation. Seems to be working. I gonna try this, will give report, how did it go! – Rozkalns Feb 23 '17 at 08:18