phplaravelseedinglaravel-jetstreamlaravel-seeding

Illuminate\Contracts\Container\BindingResolutionException : Target class [Database\Seeders\PermissionTableSeeder] does not exist


So I have a problem with seeding, the first time I got this error I thought maybe the name of the file and the table is not the same. I tried looking it up and what I found was from this link in laravel 8 with seeding , i has this issue Target class [TableSeeder] does not exist and it says to edit the namespace from Database\Seeds to Database\Seeders but that's not the problem because it's already correct. It says to check the composer.json for autoload and change Seeds to Seeders...

 "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },

but I also have the exact same thing as they suggested. And after that some people also suggested doing composer dump-autoload and this is the result....

Generating optimized autoload files
Class App\Http\Requests\UpdaUserRequest located in \app\Http\Requests\UpdateUserRequest.php does not comply with psr-4 autoloading standard. Skipping.                            
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   INFO  Discovering packages.

  jenssegers/agent ............................ DONE  
  laravel/fortify ............................. DONE  
  laravel/jetstream ........................... DONE  
  laravel/sail ................................ DONE  
  laravel/sanctum ............................. DONE  
  laravel/tinker .............................. DONE  
  livewire/livewire ........................... DONE  
  nesbot/carbon ............................... DONE  
  nunomaduro/collision ........................ DONE  
  nunomaduro/termwind ......................... DONE  
  spatie/laravel-ignition ..................... DONE  

Generated optimized autoload files containing 5496 classes

And I tried to seed it again and it gave me this....

php artisan db:seed   

   INFO  Seeding database.


   Illuminate\Contracts\Container\BindingResolutionException 

  Target class [Database\Seeders\PermissionTableSeeder] does not exist.

  at \vendor\laravel\framework\src\Illuminate\Container\Container.php:877
    873▕
    874▕         try {
    875▕             $reflector = new ReflectionClass($concrete);
    876▕         } catch (ReflectionException $e) {   

If anyone can help me, I am very grateful for anyone that can help. This is my DatabaseSeeder.php...

<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call([
            PermissionTableSeeder::class,
            RolesTableSeeder::class,
            PermissionRoleTableSeeder::class,
            UsersTableSeeder::class,
            RoleUsersTableSeeder::class,
        ]);
    }
}

and this is the PermissionsTableSeeder.php....

<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class PermissionsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $permissions = [
            [
                'id'    => 1,
                'title' => 'user_access',
            ],
            [
                'id'    => 2,
                'title' => 'task_access',
            ],
        ];

        Permissions::insert($permissions);
    }
}

Solution

  • I got the answer, so first of all apparently the name for table seeder is not right. And then I got this error...

    Error 
    
      Class "Database\Seeders\permissions" not found
    
      at \database\seeders\PermissionsTableSeeder.php:28
         24▕                 'title' => 'task_access',
         25▕             ],
         26▕         ];
         27▕
      ➜  28▕         permissions::insert($permissions);
         29▕     }
         30▕ }
         31▕
    

    and after I scour through the internet I found this https://laracasts.com/discuss/channels/laravel/laravel-8-class-databaseseedersdb-not-found. So I read the answer so you need to add use Illuminate\Support\Facades\DB; in your table seeder so it can call the database to get it to work. So now my PermissionsTableSeeder.php looks like this and it works just fine.

    <?php
    
    namespace Database\Seeders;
    
    use Illuminate\Database\Console\Seeds\WithoutModelEvents;
    use Illuminate\Database\Seeder;
    use Illuminate\Support\Facades\DB;
    
    class PermissionsTableSeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
            $permissions = [
                [
                    'id'    => 1,
                    'title' => 'user_access',
                ],
                [
                    'id'    => 2,
                    'title' => 'task_access',
                ],
            ];
    
            DB::table('permissions')->insert($permissions);
        }
    }