mardi 20 novembre 2018

Get data from pivot table after save in Laravel 5.7

I have two models User and Role

User:

public function roles() {
   $this->belongsToMany(Role::class)->withPivot('note'); 
}

Role:

public function users() {
   $this->belongsToMany(User::class); 
}

When I add another role to the user:

$role = $user->roles()->save($some_role, ['note' => 'my note']);

I would like to be able to access the note on the role that I just saved like this:

$role->note; //null 

What am I doing wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire