Laravel 5: DB Seed class not found

前端 未结 7 772
孤独总比滥情好
孤独总比滥情好 2021-02-03 21:15

I have this DatabaseSeeder.php:



        
相关标签:
7条回答
  • 2021-02-03 21:58

    I solved this by adding the class to the seeder file, with the instruction use:

    <?php
    use Illuminate\Database\Seeder;
    use Illuminate\Database\Eloquent\Model;
    use App\YourClassName;
    
    0 讨论(0)
  • 2021-02-03 21:59

    Step one - generate seed:

    php artisan make:seed MemberInvitationSeeder
    

    Step two - In DatabaseSeeder.php add line:

    $this->call(MemberInvitationSeeder::class);
    

    Step three:

    composer dump-autoload
    

    Step four:

    php artisan db:seed
    

    This should work


    If this isn't the clue, check the composer.json file and make sure you have code below in the "autoload" section:

    "classmap": [
        "database"
    ],
    
    0 讨论(0)
  • 2021-02-03 22:04

    I believe I know the reason now.

    The new class MemberInvitationSeeder wasn't in the autoloaded classes in the composer.json file.

    It wasn't there because I added that class manually.

    Now, going forward, if I add such classes again, what should I use in order for my class to automatically to the autoloader?

    0 讨论(0)
  • 2021-02-03 22:04

    You should include namespace if you want to use a string as a parameter

     $this->call('Database\Seeders\MemberInvitationSeeder');
    
    0 讨论(0)
  • 2021-02-03 22:06

    I ran into the similar error but I was using DB facade DB::table rather than model. I am posting the answer just in case somebody has similar issues.

    The seeder file had namespace namespace Database\Seeders; and laravel was trying to look for DB class inside the namespace and hence the error appeared.

    Class 'Database\Seeders\DB' not found.

    Resolutions:

    • Remove the namespace and run composer dump-autoload
    • OR Add a backslash to \DB::table('stock_categories')->([ ... ]); so Laravel starts looking the DB facade from root (not from the namespace specified)

    Eg,

    \DB::table('MemberInvitation')->insert( [
             'id' => 'BlahBlah' ,//com_create_guid(),
             'partner_id' => 1,
             'fisrt_name' => 'Thats',
             'last_name' => 'Me',
             'email' => 'me@mymail.com',
             'mobile_phone' => '444-342-4234',
             'created_at' => new DateTime
        ] );
    
    0 讨论(0)
  • 2021-02-03 22:08

    If the above solutions doesn't work, try this one. You may have changed the namespace (by default, it's "App"). What you need to do is to go to the composer.json file and check this:

    "autoload": {
          "classmap": [
              "database"
          ],
          "psr-4": {
              "App\\": "app/"
          }
      },
    

    If the namespace is App like this example, this solution is not for you.

    Otherwise, take the namespace you found and insert that line into your seeder class:

    use NameSpaceFound\User;
    
    0 讨论(0)
提交回复
热议问题