ralvez's avatar
Level 9

Illuminate\Contracts\Container\BindingResolutionException [SOLVED]

I have an app I'm developing and want to implement RBAC. I decided to use Spatie (seems like a good choice) and then created a file to set the roles and permissions:

use Illuminate\Database\Seeder; use Spatie\Permission\Models\Role; use Spatie\Permission\Models\Permission;

class RoleAndPermissionSeeder extends Seeder { public function run() { // Create permissions Permission::create(['name' => 'create items']); Permission::create(['name' => 'edit items']); Permission::create(['name' => 'delete items']); Permission::create(['name' => 'view items']);

    // Create roles
    $admin = Role::create(['name' => 'admin']);
    $editor = Role::create(['name' => 'editor']);
    $student = Role::create(['name' => 'student']);
   
    // Assign permissions to roles
    $admin->givePermissionTo(Permission::all());
    $editor->givePermissionTo('create items', 'edit items', 'view items');
    $student->givePermissionTo('view items');
}    

}

but when I run the migration to seed the database I get this error: "Illuminate\Contracts\Container\BindingResolutionException" and bellow I see: "Target class [RoleAndPermissionSeeder] does not exist."

I tried composer dump-autoload but no joy. I also tried adding the class to DatabaseSeeder.php but did not solve the issue.

Any ideas to solve this challage?

Thanks.

p.s. I'm relatively new to Laravel so may be I'm missing something obvious :)

0 likes
4 replies
LaryAI's avatar
Level 58

The Illuminate\Contracts\Container\BindingResolutionException typically occurs when the Laravel service container is unable to resolve a dependency. This can happen for a variety of reasons, such as a missing binding, a typo in a class name, or a missing service provider.

Since you're implementing Role-Based Access Control (RBAC) using the Spatie package, here are some steps to troubleshoot and resolve this issue:

  1. Ensure Spatie Package is Installed Correctly: Make sure you have installed the Spatie package correctly by running:

    composer require spatie/laravel-permission
    
  2. Publish the Configuration File: After installing the package, publish the configuration file and migration files:

    php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
    
  3. Run Migrations: Run the migrations to create the necessary tables:

    php artisan migrate
    
  4. Check Service Provider: Ensure that the Spatie\Permission\PermissionServiceProvider::class is registered in your config/app.php file under the providers array. However, if you're using Laravel 5.5 or later, this should be automatically discovered.

  5. Clear Cache: Sometimes, cached configurations can cause issues. Clear the cache using:

    php artisan config:cache
    php artisan cache:clear
    
  6. Check for Typos: Double-check your code for any typos in class names or namespaces, especially in the file where you are setting roles and permissions.

  7. Set Up Roles and Permissions: Ensure you are setting up roles and permissions correctly. Here is a basic example of how you might define roles and permissions:

    use Spatie\Permission\Models\Role;
    use Spatie\Permission\Models\Permission;
    
    // Create a role
    $role = Role::create(['name' => 'admin']);
    
    // Create a permission
    $permission = Permission::create(['name' => 'edit articles']);
    
    // Assign permission to role
    $role->givePermissionTo($permission);
    
    // Assign role to user
    $user = User::find(1);
    $user->assignRole('admin');
    
  8. Check for Middleware: If you are using middleware to check for roles or permissions, ensure it is correctly set up in your app/Http/Kernel.php file.

If you've followed these steps and are still encountering the BindingResolutionException, please provide more details about the specific error message or the code where the error occurs for further assistance.

ralvez's avatar
Level 9

I've done the things in the AI response. I'm using Laravel 11 so the point 4 about the Service Provider should not be a concern.

I do not understand why it says that I do not have the class I'm clearly using for seeding the database. Again, I'm relatively new to Laravel so may be I'm missing something. Thanks!

ralvez's avatar
Level 9

OK. I figured it out. I had created the class "by hand" instead of using "php artisan make:seeder RoleAndPermissionSeeder"

After I re-did the file using artisan it works as expected.

Snapey's avatar

@ralvez probably an incorrect namespace, however you did not include that in your posted code.

Also, please, format code in your questions by placing three backticks ``` on their own lines before and after the code

Please or to participate in this conversation.