Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sfarza's avatar

3 tables relatiosnhip with a pivot table

Hello there,

I'm trying to make a pivot tables where 3 tables are linked together.

the pivot table would be structured like this:

registre_id|intervenant_id|role_id 1 |1 | 1

registre has 2 columns: ID and name intervenant has also 2 columns : ID and name role has 2 columns too: ID and name

in registre model i've created the relationship like this:

"public function RegistreRole() { return $this->hasMany('App\Models\Role')->using('App\Models\RegistreIntervenantRole'); } public function RegistreIntervenant() {

    return $this->hasMany('App\Models\Actor')->using('App\Models\RegistreIntervenantRole');
}"

but i would like have something that replace this 2 fonctions in a single function that combines the functions above.

So my issue is how to write in the controller the request to save the ids of those this tables.

I've tried this :

"$actor->RegistreIntervenant()->attach($registre->id, $actor->id, $role->id);"

but it doesn't seems to work.

Any hint or maybe a solution that could help?

FIY: i'm a newbie in php and laravel and in programming in general. and the laravel version is 5.6

0 likes
1 reply
sfarza's avatar

it's solved, i've found a way after several trials

instead of this:

$actor->RegistreIntervenant()->attach($registre->id, $actor->id, $role->id);

I did this:

$actor->RegistreIntervenant()->attach($registre->id, array('role_id' => $role->id))

Please or to participate in this conversation.