src/Models/User.php
public function setRolesAttribute($roles): void
    {
        static::saved(function (self $model) use ($roles) {
            $roles = collect($roles)->filter();

            $model->roles->pluck('id')->similar($roles)
            || activity()
                ->performedOn($model)
                ->withProperties(['attributes' => ['roles' => $roles], 'old' => ['roles' => $model->roles->pluck('id')->toArray()]])
                ->log('updated');

            $model->roles()->sync($roles, true);
        });
    }
public function setRolesAttribute($roles): void
    {
        static::saved(function (self $model) use ($roles) {
            $roles = collect($roles)->filter();

            $model->roles->pluck('id')->similar($roles)
            || activity()
                ->performedOn($model)
                ->withProperties(['attributes' => ['roles' => $roles], 'old' => ['roles' => $model->roles->pluck('id')->toArray()]])
                ->log('updated');

            $model->roles()->sync($roles, true);
        });
    }
public function setRolesAttribute ( $ roles ) : void { static :: saved ( function ( self $ model ) use ( $ roles ) { $ roles = collect ( $ roles ) -> filter ( ) ; $ model -> roles -> pluck ( 'id' ) -> similar ( $ roles ) || activity ( ) -> performedOn ( $ model ) -> withProperties ( [ 'attributes' => [ 'roles' => $ roles ] , 'old' => [ 'roles' => $ model -> roles -> pluck ( 'id' ) -> toArray ( ) ] ] ) -> log ( 'updated' ) ; $ model -> roles ( ) -> sync ( $ roles , true ) ; } ) ; }
Attach the given roles to the model.

@param mixed $roles

@return void
Attach the given roles to the model .