Laravel doesn't work mutator for field in model pivot

60 views Asked by At

I have two models linked by a pivot table. I want to make a mutator in the pivot table for the user_id field. My models

class User extends Model
{
     public function roles()
     {
        $this->belongsToMany(
            Role::class,
            'role_user',
            'user_id',
            'role_id'
        )->withPivot('status')->using(UserRole::class);
     }
}

class Role extends Model
{
     public function users()
     {
        $this->belongsToMany(
            User::class,
            'role_user',
            'role_id',
            'user_id'
        )->withPivot('status')->using(UserRole::class);
     }
}

My pivot table

class UserRole extends Pivot
{
    protected $fillable = [
        'id',
        'role_id',
        'user_id',
        'status'
    ];
}

If I specify the mutator name

public function setStatusAttribute($value){
    dd("Hello");
}

it's work, but if I specify the mutator name

public function setUserIdAttribute($value){
    dd("Hello");
}

What could be wrong?

0

There are 0 answers