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');
}
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With