I'm trying to join 2 tables, many to many, but I get an inner join error where there seems to be a missing id, but how can I pass that id?
Events to UserEvents
sql error when doing get in the last part, apparently it does not receive an id :
Unknown column 'event_school_user.' in 'on clause'
(SQL: select count(*) as aggregate from `event_schools` inner join `event_school_user` on `event_schools`.`id` = `event_school_user`.``
ModelUser:
public function event_schools() {
return $this->belongsToMany(EventSchool::class)->using(EventSchoolUser::class);
}
EventSchool: here I think it may be the error
class EventSchool extends Pivot
{
protected $table = 'event_schools';
protected $primaryKey = 'id';
use Notifiable;
use SoftDeletes;
public $fillable = ['event_school_id','user_id'];
protected $dates = ['deleted_at'];
public function event_schools() {
return $this->belongsToMany(EventSchool::class);
}
public function users() {
return $this->belongsToMany(User::class);
}
}
EventSchoolUser:
class EventSchoolUser extends Model
{
protected $table = 'event_school_user';
}
This is an example of the relations of my table, they are the same, only the names of the tables change:
For now what I try is to do a Select * from events to the events table(EventSchool): something like this
$events = $user->event_schools()->paginate(12);
Although it is true that Laravel allows the work of a model for the intermediate table , the above does not seem necessary to me and if we reformulate it I would think that you achieve the expected result in a simple way.
So based on the above I would delete the model
EventSchool
Then there should only be 2 models left ( I vpy based on the example you provide ):
In each of the models you should declare the relationship that links them with the intermediate table:
Model
Usuario
Model
Evento
After the above, if you want to get all the events of a user, you can write the query in this way:
Final comment: