whatitis2
whatitis2

Reputation: 159

laravel 5.8 "Call to undefined method car:SetContainer()" when using php artisan migrate refresh

I am playing around with laravel and am trying to make a model named car, factory named car, and to seed it with 3 different car makes randomly. I have gotten up to the point where I am trying to seed the table and I keep getting a SetContainer() error

    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.

I will give you my files below

Here is: 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);
    }
}

Here is: app/car.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

Here is: 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']),
    ];
});

Here is: 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());
        });
    }
}

Here is 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');
    }
}

Upvotes: 15

Views: 11431

Answers (3)

saber tabatabaee yazdi
saber tabatabaee yazdi

Reputation: 4959

my mistake is to use Model instead of Seeder

just remove this:

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

and use this:

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

Upvotes: 21

Adonai Nangui
Adonai Nangui

Reputation: 571

I think your issue is that you are using Model instead of Seeder, you must replace that like on the code below 👇🏾

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

Upvotes: 53

Theekshana
Theekshana

Reputation: 417

There are few errors I saw in your code, please apply below modifications to your files and give a try.

STEP 01: modify the migrations/2019_07_27_car.php file

you are not providing the values for model and year field in the car table hence above fields should be nullable, therefore I added nullable() method for those two fields.

<?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 modify the app/car.php file

you are not providing the $table->timestamps() for car migration file, you should add the public $timestamps = false field to your car model.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

STEP 03 modify the factories/car.php

As I felt you simply want to create 50 entries for car table, hence car seeder file can simpy run factory class like below.

<?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 run the artisan command

after above modifications run the artisan command. $ php artisan migrate:fresh --seed

Upvotes: 1

Related Questions