Laravel 5 - artisan seed [ReflectionException] Cla

2019-01-16 01:53发布

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);

    }

}

12条回答
forever°为你锁心
2楼-- · 2019-01-16 02:12

What I did is to recreate the Seeder again and it works for me.

php artisan make:seeder UsersTableSeeder

查看更多
\"骚年 ilove
3楼-- · 2019-01-16 02:19

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.

查看更多
倾城 Initia
4楼-- · 2019-01-16 02:20

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
查看更多
祖国的老花朵
5楼-- · 2019-01-16 02:21

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)]);

    }

}

}
查看更多
Ridiculous、
6楼-- · 2019-01-16 02:27

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.

查看更多
女痞
7楼-- · 2019-01-16 02:28

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.

查看更多
登录 后发表回答