Laravel 5.8使用php artisan migrate refresh时出现“Call to undefined method car:SetContainer()”错误

13

我正在研究Laravel,尝试创建名为"car"的模型、名为"car"的工厂,并随机添加三种不同的汽车品牌。我已经进行到了填充表格的步骤,但一直出现“SetContainer()”错误。

    Seeding: car

   Symfony\Component\Debug\Exception\FatalThrowableError  : Call to undefined method car::setContainer()

  at /home/bob/myproject/vendor/laravel/framework/src/Illuminate/Database/Seeder.php:70
    66|     {
    67|         if (isset($this->container)) {
    68|             $instance = $this->container->make($class);
    69| 
  > 70|             $instance->setContainer($this->container);
    71|         } else {
    72|             $instance = new $class;
    73|         }
    74| 

  Exception trace:

  1   Illuminate\Database\Seeder::resolve("car")
       /home/bob/myproject/vendor/laravel/framework/src/Illuminate/Database/Seeder.php:42

  2   Illuminate\Database\Seeder::call("car")
      /home/bob/myproject/database/seeds/DatabaseSeeder.php:15

  Please use the argument -v to see more details.

我会在下面给你我的文件

这里是:DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UsersTableSeeder::class);
        $this->call(car::class);
    }
}


这里是:app/car.php。
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class car extends Model
{
    protected $fillable = ['make','model','year'];
}

这里是:工厂/car.php。
<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */

use App\car;
use Faker\Generator as Faker;

$factory->define(App\car::class, function (Faker $faker) {
    return [
        'make' => $faker->randomElement(['ford', 'honda','toyota']),
    ];
});

这里是:seeds/car.php

<?php

use Illuminate\Database\Seeder;

class car extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\car::class, 50)->create()->each(function ($car) {
            $car->car()->save(factory(App\car::class)->make());
        });
    }
}

这里是迁移/migrations/2019_07_27_car.php文件。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class car extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('car', function (Blueprint $table) {
            $table->string('make');
            $table->string('model');
            $table->string('year');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('car');
    }
}


try composer dumpautoload - LulzAsh
尝试了这个,运行 "php artisan migrate:refresh --seed" 问题仍然存在。 - whatitis2
请确保将 car 改为大写的 Car - PtrTon
所有我的文件都是用小写字母c命名的,但我尝试过这种方式,错误还没有消失。您有其他的想法吗? - whatitis2
3个回答

43

我认为你的问题在于你使用了Model而不是Seeder,你需要像下面的代码那样替换

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UsersTableSeeder::class);
        $this->call(CarSeeder::class);
    }
}

2
你很棒。谢谢。 - Abhishek Kumar
2
太棒了。它解决了问题。 - Azlan Jamal

20
我的错误是使用了Model而不是Seeder 只需删除这个:
    $this->call([        PackageAnswer::class,    ]);

并使用这个:

    $this->call([        PackageAnswerSeeder::class,    ]);

1
我也是,非常感谢。 - Vipertecpro
1
15分钟找这个愚蠢的错误。谢谢,这个答案至少节省了我另外30分钟才能自己找到它!!! - Fryla- Cristian Marucci

1

我发现你的代码中有一些错误,请按照以下修改对文件进行修改并尝试。

步骤01:修改migrations/2019_07_27_car.php文件

你没有为汽车表中的modelyear字段提供值,因此上述字段应为空,因此我为这两个字段添加了nullable()方法。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class car extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('car', function (Blueprint $table) {
            $table->string('make');
            $table->string('model')->nullable();
            $table->string('year')->nullable();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('car');
    }
}

步骤二 修改 app/car.php 文件

你在汽车迁移文件中没有提供 $table->timestamps(),你应该在汽车模型中添加 public $timestamps = false 字段。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class car extends Model
{
    protected $fillable = ['make','model','year'];
    public $timestamps = false;
}

步骤 03 修改 factories/car.php

由于我认为您只是想为汽车表创建50个条目,因此汽车播种机文件可以简单地运行以下工厂类。

<?php

use Illuminate\Database\Seeder;

class car extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
         factory(App\car::class, 50)->create();
    }
}

第四步 运行artisan命令

在以上修改完成后,运行artisan命令。 $ php artisan migrate:fresh --seed


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接