robspin
robspin

Reputation: 811

{closure}() must be an instance of Faker\Generator\Generator on laravel factory seed

when I use php artisan db:seed I got the error? In UserFactory.php line 27:

Type error: Argument 1 passed to
Illuminate\Database\Eloquent\Factory::{closure}() must be an instance
of Faker\Generator\Generator, instance of Faker\Generator given

Userfactory.php use Faker\Generator as Faker;

(this is line 27)

$factory->define(App\Models\Admin::class, function (Faker\Generator $faker) {
    static $password;

        return [
            'name' => $faker->name,
            'email' => $faker->safeEmail,
            'password' => $password ?: $password = bcrypt('secret'),
            'remember_token' => str_random(10),
        ];
    });

AdminsTableSeeder.php

public function run()
{
    factory('App\Models\Admin',3)->create([
        'password' => bcrypt('123456')
        ]);
}

DatabaseSeeder.php

public function run()
{
    // $this->call(UsersTableSeeder::class);
    $this->call(AdminsTableSeeder::class);
}

I don't know how it happens.

Upvotes: 2

Views: 4485

Answers (1)

robspin
robspin

Reputation: 811

I solved it use Faker\Generator as Faker;

(this is line 27)
    $factory->define(App\Models\Admin::class, function (Faker $faker) 

That means that instead of using function(Faker\Generator $faker) in your function, instead use function (Faker $faker)

That solved it for me.

Upvotes: 7

Related Questions