【问题标题】:laravel 5.8 "Call to undefined method car:SetContainer()" when using php artisan migrate refresh使用 php artisan migrate refresh 时,laravel 5.8“调用未定义的方法 car:SetContainer()”
【发布时间】:2019-12-05 17:10:25
【问题描述】:

我正在玩 laravel,正在尝试制作一个名为汽车的模型,一个名为汽车的工厂,并用 3 种不同的汽车制造随机播种。我已经到了尝试为表播种的地步,但我不断收到 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'];
}

这里是:factories/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());
        });
    }
}

这里是迁移/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');
    }
}

【问题讨论】:

  • 试试 composer dumpautoload
  • 试过这个,当运行“php artisan migrate:refresh --seed”问题仍然存在。
  • 确保大写Car而不是car
  • 我所有的文件都是小写的 c 但我试过了,它并没有使错误消失。您还有其他想法吗?

标签: php database laravel


【解决方案1】:

我认为您的问题是您使用的是 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】:

我的错误是使用 Model 而不是 Seeder

删除这个:

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

并使用它:

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

【讨论】:

  • 是的,我也是,谢谢很多
  • 15 分钟寻找这个愚蠢的错误。谢谢,这个答案至少让我再节省 30 分钟,直到我自己找到它!!!
【解决方案3】:

我在您的代码中看到了一些错误,请对您的文件应用以下修改并试一试。

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

您没有在 car 表中提供 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');
    }
}

STEP 02 修改app/car.php file

您没有提供$table-&gt;timestamps() 用于汽车迁移文件,您应该将 public $timestamps = false 字段添加到您的汽车型号。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

STEP 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();
    }
}

STEP 04 运行 artisan 命令

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

【讨论】:

    猜你喜欢
    • 2021-02-28
    • 2020-01-20
    • 2017-04-08
    • 2019-11-02
    • 1970-01-01
    • 2017-04-06
    • 2020-10-23
    • 2018-08-04
    • 2016-08-09
    相关资源
    最近更新 更多