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
Where does the
user_id
live in this schema?
(which table has a user_id
column?)the pivot table service_hours ( ServiceModel) has
/
* 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
/ * 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?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?Yes a volunteer (user) belongsTo a supervisor (User)
Ok, makes sense
and a supervisor belongs to a volunteer
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?
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.
as in https://laravel.com/docs/10.x/eloquent-relationships#defining-custom-intermediate-table-models
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.
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