phpcastingaccessorlaravel-10

How to create an accessor for pivot field in larval 10.x?


I have two models:

i add this method but it doesn't work and return string:

    public function getTypeAttribute($value){
        return collect(json_decode($value));
    }

Solution

  • I forgot to use using. It should be like this: $this->belongsToMany(Place::class, 'roles', 'place_id', 'user_id')->using(Role::class)->withPivot('type');