Hi qace,
I encountered the exact same issue where the error message pops up whenever I try to perform a $role->delete(). I wasn't able to fix the problem - but I found a workaround. To do this, I simply wrote an extension model for the Role model that overrides the delete function.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\Permission\Models\Role;
class RoleExtend extends Role
{
/**
* Delete the role and detach all related permissions and users.
*
* @return bool|null
* @throws \Exception
*/
public function delete()
{
\DB::beginTransaction();
try {
\DB::table('model_has_roles')
->where('role_id', $this->id)
->delete();
\DB::table('role_has_permissions')
->where('role_id', $this->id)
->delete();
\DB::table('roles')
->where('id', $this->id)
->delete();
\DB::commit();
} catch (\Exception $e) {
\DB::rollback();
throw $e;
}
}
}
It's not the cleanest solution, but it buys me some time to deal with it later.