Home > Software design >  laravel spatie syncpermission not working
laravel spatie syncpermission not working

Time:04-18

i had a problem with syncPermission to role on laravel when storing data to database,the error looks like this "Call to undefined method Spatie\Permission\Models\Role::syncPermission()", when i use this method on seeders its work properly. maybe im missing something important. so i need your help to figure it out. thankyou in advance

so this is my controller code

    public function store(Request $request){
    $validator = Validator::make($request->all(),[
        'name' => 'required|unique:roles,name',
        'permission' => 'required|min:1'
    ]);

    if ($validator->fails()) {
        return response()->json($validator->errors(), 422);
    }

    $role = Role::create([
        'name' => $request->name,
        'guard_name' => 'api'
            
    ]);
    $role->syncPermission($request->permission);

    if($role) {
        //return success with Api Resource
        return new RoleResource(true, 'Succes', $role);
    }

    //return failed with Api Resource
    return new RoleResource(false, 'Failed', null);

}

CodePudding user response:

It will be like:

$role->syncPermissions($request->permission);

Not

$role->syncPermission($request->permission);

Just use : syncPermissions (you missed 's')

  • Related