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

marbobo's avatar
Level 12

Laravel 11 Cannot save permissions to role

Hello everyone,

I was following an old Laracasts video and encountered an issue while trying to save permissions to a role. Here's the scenario:

I have three tables: roles, permissions, and permission_roles, each corresponding to a model in my Laravel application.

In my Role model, I have defined a permissions relationship like this:

public function permissions(): BelongsToMany
{
    return $this->belongsToMany(Permission::class);
}

public function givePermission(Permission $permission)
{
    return $this->permissions()->save($permission);
}

However, when I try to use the givePermission method, I encounter the following error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.permission_role' doesn't exist 
(Connection: mysql, SQL: insert into `permission_role` (`permission_id`, `role_id`) values (1, 1)).

I have already set protected $table = "permission_roles"; on my PermissionRole model, but the error persists.

Any insights or suggestions on how to resolve this issue would be greatly appreciated.

Thank you!

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It seems like you're encountering an issue with the naming convention that Laravel expects for pivot tables. By default, Laravel assumes that the pivot table name is the singular form of the two related model names in alphabetical order. In your case, it should be permission_role instead of permission_roles.

However, since you mentioned that you have set the $table property in your PermissionRole model, it seems you might be using a custom pivot model, which is not necessary for many-to-many relationships unless you need to add additional behavior to the pivot table.

Here's how you can resolve the issue:

  1. If you are not using a custom pivot model and just want to define a many-to-many relationship, you don't need a PermissionRole model. Instead, make sure your pivot table is named correctly (permission_role) and remove the $table property from your non-existent PermissionRole model.

  2. If you have a specific reason to use a custom pivot model (like adding additional attributes or methods), you need to specify this custom pivot model in your relationship definition using the using method:

public function permissions(): BelongsToMany
{
    return $this->belongsToMany(Permission::class)
                ->using(PermissionRole::class);
}

Make sure that your PermissionRole model extends the Illuminate\Database\Eloquent\Relations\Pivot class.

  1. If you are not using a custom pivot model and you want to keep the table name permission_roles, you need to specify the custom table name in your relationship method:
public function permissions(): BelongsToMany
{
    return $this->belongsToMany(Permission::class, 'permission_roles');
}

This tells Laravel to use permission_roles as the pivot table name instead of the default permission_role.

  1. Lastly, ensure that your pivot table exists in the database with the correct name and structure. If it doesn't exist, you'll need to create a migration to generate it:
Schema::create('permission_role', function (Blueprint $table) {
    $table->unsignedBigInteger('permission_id');
    $table->unsignedBigInteger('role_id');
    $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
    $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
    $table->primary(['permission_id', 'role_id']);
});

After making these changes, try running your givePermission method again, and it should work without any issues. Remember to run your migrations if you've made changes to the database schema.

1 like

Please or to participate in this conversation.