pivot table user/user

Anyone have an example of a pivot table that uses User on both sides of the BelongsToMany Volunteer/Coordinator. where any user can be either role I would use this in two relation Managers under the User Service Hours this user is volunteering Supervised Hours this use is Coordinating
10 Replies
Patrick Boivin
Where does the user_id live in this schema? (which table has a user_id column?)
ddoddsr
ddoddsrOP2y ago
the pivot table service_hours ( ServiceModel) has
$table->foreignId('user_id');
$table->foreignId('supervisor_id');
$table->foreignId('department_id');
On the User Model
$table->foreignId('user_id');
$table->foreignId('supervisor_id');
$table->foreignId('department_id');
On the User Model
/ * The users that belong to the serviceHour . */ public function serviceUser(): BelongsToMany { return $this->belongsToMany(User::class)->using(ServiceHour::class, 'user_id'); }
/
* The supervisors that belong to the serviceHour . */ public function serviceSupervisor(): BelongsToMany { return $this->belongsToMany(User::class)->using(ServiceHour::class, 'supervisor_id'); } I get the error Table 'testtrust.user_user' doesn't exist. which tells me that I still don't understand. Does the relation protected static string $relationship = 'serviceSupervisor'; point to the ServiceHour Model or User Model?
Patrick Boivin
Yes, I think this makes sense... If on the User model you have : $this->belongsToMany(User::class) ... you're saying that a user belongs to a user. Or am I missing something?
ddoddsr
ddoddsrOP2y ago
Yes a volunteer (user) belongsTo a supervisor (User)
Patrick Boivin
Ok, makes sense
ddoddsr
ddoddsrOP2y ago
and a supervisor belongs to a volunteer
Patrick Boivin
So in your app, all of this is working for you? Are you having any issues or you are wondering how to integrate this into Filament?
ddoddsr
ddoddsrOP2y ago
not working, somehow I need to tell eloquent and maybe Filament that I am going through the ServiceHour model rather than using the default naming.
ddoddsr
ddoddsrOP2y ago
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Patrick Boivin
Yeah, definitely make sure it's working in standard Laravel before trying to add this to a relation manager. I'll have a look in the docs with you, my memory is not too fresh on pivot tables tbh

Did you find this page helpful?