Laravel 5: DB Seed class not found

44,380

Solution 1

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?

Solution 2

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"
],

Solution 3

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;

Solution 4

This work for me

  1. composer dump-autoload
  2. php artisan db:seed

Solution 5

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' => '[email protected]',
         'mobile_phone' => '444-342-4234',
         'created_at' => new DateTime
    ] );
Share:
44,380
JasonGenX
Author by

JasonGenX

My passion is solving real world problems with code. Always learning new languages/frameworks - hence why I'm here.

Updated on December 05, 2020

Comments

  • JasonGenX
    JasonGenX over 3 years

    I have this DatabaseSeeder.php:

    <?php
    
    use Illuminate\Database\Seeder;
    use Illuminate\Database\Eloquent\Model;
    
    
    
        class DatabaseSeeder extends Seeder {
    
            /**
             * Run the database seeds.
             *
             * @return void
             */
            public function run()
            {
                Model::unguard();
    
                $this->call('MemberInvitationSeeder');
            }
        }
    

    I have this file MemberInvitationSeeder.php, sibling to the DatabaseSeeder.php file

    <?php
    
    use Illuminate\Database\Seeder;
    use App\MemberInvitation;
    
        class MemberInvitationSeeder extends Seeder {
    
            public function run()
            {
                MemberInvitation::truncate();
    
                MemberInvitation::create( [
                    'id' => 'BlahBlah' ,//com_create_guid(),
                    'partner_id' => 1,
                    'fisrt_name' => 'Thats',
                    'last_name' => 'Me',
                    'email' => '[email protected]',
                    'mobile_phone' => '444-342-4234',
                    'created_at' => new DateTime
                ] );
    
            }
        }
    

    Now I call

    php artisan db:seed
    

    and I get:

    [ReflectionException]                        
      Class MemberInvitationSeeder does not exist
    

    I tried everything I could find including "composer dump-autoload". to no avail. What am I doing wrong?