Hi,
I have 3 tables. Users, Events and Reservations(rsvps).
I would like to return back all events where there are no reservation for user 1. Basically allowing user 1 to see all events he does not have a reservation for.
User Model
class User extends Authenticatable
{
protected $with = [
'events',
'reservations',
];
public function events() {
return $this->hasMany('App\EventModel');
}
public function reservations() {
return $this->hasMany('App\Rsvp');
}
}
Event Model
class EventModel extends Model
{
protected $table = 'events';
protected $with = [
'rsvps'
];
public function rsvps()
{
return $this->hasMany('App\Rsvp', 'event_id');
}
}
Resveration(rsvp)
class Rsvp extends Model
{
public function user()
{
return $this->belongsTo('User');
}
public function event()
{
return $this->belongsTo('App\EventModel');
}
}
I tried something like this.
$newEvents = EventModel::where('rsvps.user_id', '!=', 1)->get();
Events will have an rsvps array with multiple object entries because its a 1-to-many. So my above logic is a bit flawed. Any help?