【问题标题】:php artisan db:seed is not working Laravel 5php artisan db:seed 不工作 Laravel 5
【发布时间】:2015-11-11 08:10:51
【问题描述】:

我已阅读其他答案,但没有解决我的问题。当我运行 php artisan db:seed 时没有任何反应,甚至没有抛出错误。
这是我的 DatabaseSeeder 类

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

        // call our class and run our seeds
        $this->call(BearAppSeeder::class);

        // $this->call(UserTableSeeder::class);

        Model::reguard();
    }
}

BearAppSeeder 包含以下内容。

<?php

use Illuminate\Database\Seeder;

class BearAppSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // clear our database ------------------------------------------
        /*DB::table('bears')->delete();
        DB::table('fish')->delete();
        DB::table('picnics')->delete();
        DB::table('trees')->delete();
        DB::table('bears_picnics')->delete();*/

        // seed our bears table -----------------------
        // we'll create three different bears

        // bear 1 is named Lawly. She is extremely dangerous. Especially when hungry.
        $bearLawly = Bear::insert(['name'         => 'Lawly',
            'type'         => 'Grizzly',
            'danger_level' => 8]);

        /*/ bear 2 is named Cerms. He has a loud growl but is pretty much harmless.
        $bearCerms = Bear::create(array(
            'name'         => 'Cerms',
            'type'         => 'Black',
            'danger_level' => 4
        ));

        // bear 3 is named Adobot. He is a polar bear. He drinks vodka.
        $bearAdobot = Bear::create(array(
            'name'         => 'Adobot',
            'type'         => 'Polar',
            'danger_level' => 3
        ));

        $this->command->info('The bears are alive!');

        // seed our fish table ------------------------
        // our fish wont have names... because theyre going to be eaten

        // we will use the variables we used to create the bears to get their id

        Fish::create(array(
            'weight'  => 5,
            'bear_id' => $bearLawly->id
        ));
        Fish::create(array(
            'weight'  => 12,
            'bear_id' => $bearCerms->id
        ));
        Fish::create(array(
            'weight'  => 4,
            'bear_id' => $bearAdobot->id
        ));

        $this->command->info('They are eating fish!');

        // seed our trees table ---------------------
        Tree::create(array(
            'type'    => 'Redwood',
            'age'     => 500,
            'bear_id' => $bearLawly->id
        ));
        Tree::create(array(
            'type'    => 'Oak',
            'age'     => 400,
            'bear_id' => $bearLawly->id
        ));

        $this->command->info('Climb bears! Be free!');

        // seed our picnics table ---------------------

        // we will create one picnic and apply all bears to this one picnic
        $picnicYellowstone = Picnic::create(array(
            'name'        => 'Yellowstone',
            'taste_level' => 6
        ));
        $picnicGrandCanyon = Picnic::create(array(
            'name'        => 'Grand Canyon',
            'taste_level' => 5
        ));

        // link our bears to picnics ---------------------
        // for our purposes we'll just add all bears to both picnics for our many to many relationship
        $bearLawly->picnics()->attach($picnicYellowstone->id);
        $bearLawly->picnics()->attach($picnicGrandCanyon->id);

        $bearCerms->picnics()->attach($picnicYellowstone->id);
        $bearCerms->picnics()->attach($picnicGrandCanyon->id);

        $bearAdobot->picnics()->attach($picnicYellowstone->id);
        $bearAdobot->picnics()->attach($picnicGrandCanyon->id);

        $this->command->info('They are terrorizing picnics!');*/
    }
}

我尝试过运行composer dump-autoload,但这并没有解决我的问题。我是 Laravel 的新手,所以我不太了解它。

【问题讨论】:

    标签: php laravel-5.1 seeding laravel-seeding


    【解决方案1】:

    我认为在BearAppSeeder 中找不到您的模型类Bear。所以 , 放

    use App\Bear;

    之后

    use Illuminate\Database\Seeder;

    希望对你有帮助

    【讨论】:

    • 想知道为什么直到现在还没有人更新这个答案。无论如何谢谢你:)。
    【解决方案2】:

    通过以下方式调用种子类:

     $this->call(App\BearAppSeeder::class);
    

    在 BearAppSeeder::class 之前使用 App 目录名

    【讨论】: