Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kevinwakhisi's avatar

Call to a member function givePermissionTo() on int

public function edit(Request $request, $id)
    {
        //TODO: Cannot update error -  Call to a member function syncPermissions() on int
        //edit the role created
        $this->validate($request, [
            'name' => 'required',
            'permission' => 'nullable'
        ]);
        $role = Role::where('id', $id)->update([
            'name' => $request->name,
        ]);
        if ($request->has('permission')) {
            $role->givePermissionTo($request->permission);
        }
        return $role;
    }

i am trying to edit my roles and permissions using the code above, using Vue framework as the front end and this package is from spatie

the error on my response is

 "message": "Call to a member function givePermissionTo() on int",
    "exception": "Error",

my overall code works fine just the edit part that doesn't change

0 likes
6 replies
kevinwakhisi's avatar

i am still getting the same error. when i don't include the permission works fine but all i want is to also update the permissions that each role has

MichalOravec's avatar
Level 75

Oh ok, my bad, this will work

$role = Role::findOrFail($id);

$role->update([
    'name' => $request->name
]);

if ($request->has('permission')) {
    $role->givePermissionTo($request->permission);
}
3 likes
kevinwakhisi's avatar

worked. Damn what would have caused the error?Kindly if you would explain. I really do appreciate

MichalOravec's avatar

update() in query builder return int. Which is the number of rows affected.

1 like

Please or to participate in this conversation.