324

When I run php artisan db:seed I am getting the following error:

[ReflectionException] Class SongsTableSeeder does not exist

What is going on?

My DatabaseSeeder class:

<?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('SongsTableSeeder');
    }

}

My SongsTableSeeder class:

<?php

// Composer: "fzaninotto/faker": "v1.4.0"
use Faker\Factory as Faker;
use Illuminate\Database\Seeder;
use DB;

class SongsTableSeeder extends Seeder {

    public function run()
    {
        $faker = Faker::create();
        $songs = [];
        foreach(range(1, 10) as $index)
        {
            $songs[] = ['title' => $faker->words(rand(1,4))];
        }

        DB::table('songs')->insert($songs);

    }

}
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Sasha
  • 8,521
  • 23
  • 91
  • 174

15 Answers15

874

You need to put SongsTableSeeder into file SongsTableSeeder.php in the same directory where you have your DatabaseSeeder.php file.

And you need to run in your console:

composer dump-autoload

to generate new class map and then run:

php artisan db:seed

I've just tested it. It is working without a problem in Laravel 5

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • 1
    I hadn't been 'use'ing the model in my seeder class. Works fine now. – Jamie Poole May 09 '15 at 16:46
  • 2
    `composer dumpautoload` also flies – Connor Leech Jul 14 '17 at 16:28
  • 1
    composer dump-autoload solved the problem to me. what composer dump-autoload really does? – Luís Almeida Sep 19 '17 at 11:26
  • 8
    @LuísAlmeida `dump-autoload` regenerates the list of all classes that need to be included in the project, it's used when you have a new class inside your project. Normally running `make:*` will add the newly created class, but this won't occur if you manually create a seeder. At least that's how I understand it to work from 5 minutes of searching. I'm not an expert. – Xander Luciano Mar 10 '18 at 03:27
  • 2
    I tried this solution with Laravel 5.8 on a development server and `composer dump-autoload` worked for me. – Mycodingproject Dec 08 '19 at 23:41
25

I solved it by doing this:

  1. Copy the file content.
  2. Remove file.
  3. Run command: php artisan make:seeder .
  4. Copy the file content back in this file.

This happened because I made a change in the filename. I don't know why it didn't work after the change.

silver est
  • 51
  • 6
Ron van Asseldonk
  • 1,275
  • 2
  • 11
  • 18
  • 3
    Probably running composer dump-auto was easier. The autoloader is now looking for an older file. So if you dump this file via command line, it will generate a new autoloader file with the new file. So in the future try this =) – Ron van Asseldonk Apr 06 '16 at 13:51
  • For more information about composer autoloader see this site: https://getcomposer.org/doc/01-basic-usage.md#autoloading – Ron van Asseldonk Apr 06 '16 at 14:31
  • 1
    van Asseldok, well, `composer dump-autoload` does not work for me. I must to recreate seeder class to make it working. – М.Б. Nov 10 '17 at 09:24
  • This helped me. ‘composer dump-autoload’ does not work for me as well, something to do with my server settings, or the fact that it’s a shared sever. – Derk Jan Speelman Mar 22 '19 at 06:54
22

File SongsTableSeeder.php should be in database/seeds directory or in its subdirectory.

You need to run:

composer dump-autoload

and then:

php artisan db:seed

or:

php artisan db:seed --class=SongsTableSeeder
simhumileco
  • 31,877
  • 16
  • 137
  • 115
  • 1
    able to see **Parse Error** with 'php artisan db:seed --class=SongsTableSeeder' command. thnks!! – Omkar Oct 18 '17 at 07:09
17

If you migrated to Laravel 8, you have to add a namespace to the seeders class:

<?php

namespace Database\Seeders;

...

Next, in your composer.json file, remove classmap block from the autoload section and add the new namespaced class directory mappings:

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

An finally, do a composer dump-autoload.

For more information: https://laravel.com/docs/8.x/upgrade#seeder-factory-namespaces

pableiros
  • 14,932
  • 12
  • 99
  • 105
9

SongsTableSeeder.php should be in database/seeds directory

Console command steps:

composer dump-autoload

and then:

php artisan cache:clear

and then:

php artisan optimize

and then:

php artisan db:seed

or:

php artisan db:seed --class=SongsTableSeeder
4

I'm running the very latest Laravel 5 dev release, and if you've changed the namespace you'll need to call your seed class like this:

$this->call('\todoparrot\TodolistTableSeeder');

Obviously you'll need to replace todoparrot with your designated namespace. Otherwise I receive the same error indicated in the original question.

Jason Gilmore
  • 3,698
  • 3
  • 23
  • 28
3

Do not forgot that the composer dump-autoload works in relation with the autoload / classmap section of composer.json. Take care about that if you need to change seeders directory or use multiple directories to store seeders.

"autoload": {
    "classmap": [
      "database/seeds",
      "database/factories"
    ],
},
abenevaut
  • 768
  • 1
  • 9
  • 23
3

You probably specify the .php extension and It don't found your class.

What I was doing :

php artisan db:seed --class=RolesPermissionsTableSeeder.php

What solved my problem : What I was doing :

php artisan db:seed --class=RolesPermissionsTableSeeder
Axel Paris
  • 303
  • 3
  • 7
1

When you migrate your project to Laravel 8 and you get that error then you should follow some steps given below.

1.Go to your composer.json file.

2.Change autoload section

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

to

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

3.Now run the command of composer dump-autoload or composer update

4.And last run command of php artisan db:seed

Krina Mangukiya
  • 366
  • 2
  • 7
0

If our CustomTableSeeder is in same directory with DatabaseSeeder we should use like below:

$this->call('database\seeds\CustomTableSeeder');

in our DatabaseSeeder File; then another error will be thrown that says: 'DB Class not found' then we should add our DB facade to our CustomTableSeeder File like below:

use Illuminate\Support\Facades\DB;

it worked for me!

0

I have used only SINGLE FILE with TWO classes in it following :

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\Lesson;

use Faker\Factory as Faker;

class DatabaseSeeder extends Seeder {

/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    //Lesson::truncate();

    Model::unguard();

    $this->call("LessonsTableSeeder");


}

}

class LessonsTableSeeder extends Seeder {

/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{

    $faker = Faker::create();

    foreach(range(1,30) as $index) {

        Lesson::create(['title' => $faker->sentence(5), 'body' => $faker->paragraph(4)]);

    }

}

}
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51
0

If you have copied the seeders files from any other project then you need to run the artisan command php artisan db:seed otherwise it is fine.

Shahid Hussain
  • 157
  • 1
  • 1
  • 8
0

i got [ReflectionException] Class Seeder does not exist too and when i use composer dump-autoload, i got an error preg_match(): JIT compilation failed: no more memory when i run it.

What i did is that i change ;pcre.jit=1 to pcre.jit=Off in php.ini! You can find the path by using php --ini in your terminal!

I am using mac with php 7.3! Hope that help any of you guys out there!

Gerald H
  • 454
  • 4
  • 13
0

Simply change:

$this->call('SongsTableSeeder')

to

$this->call(SongsTableSeeder::class)

-5

I had the same "reflection exception" error. The solution was to copy the class file to the server, from dev, for me. dumb mistake, but given how many files we deal with its easy to forget to copy them over to the server every time.

James Danforth
  • 781
  • 7
  • 7
  • 2
    This response is very vague and is coupled with your specific hosting setup. Although your input is valued this may not help developers looking for solutions to this issue. I think your issue is caused by you leaving it down to humans to copy files to the server. It's encouraged to have a CI pipeline where files are automatically deployed to the server :) – charj Jun 17 '19 at 09:30