I'm trying to seed using factories in Laravel 5.2
My code dies in the User factory:
$factory->define(App\User::class, function (Faker\Generator $faker) {
$countries = Countries::all()->pluck('id')->toArray();
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => bcrypt(str_random(10)),
'grade_id' => $faker->numberBetween(1, 5),
'country_id' => $faker->randomElement($countries),
'city' => $faker->city,
'latitude' => $faker->latitude,
'longitude' => $faker->longitude,
'role_id' => $faker->numberBetween(1, 3),
'verified' => true,
'remember_token' => str_random(10),
'provider' => '',
'provider_id' => str_random(5)
];
});
Giving me this error:
A four digit year could not be found Data missing
I found the cause, but don't know how to fix it.
When I call the factory, I call it like that:
factory(User::class)->create(['role_id',2]);
If I call it like that:
factory(User::class)->create();
I get no more error. But I really need to seed different kind of users...
Any idea???
have you tried using key value array in the create
method:
factory(User::class)->create(['role_id' => 2]);
I might be late to the party, I was having the same problem and it turns out its because I provided a key without a value in the array returned.
get rid of 'provider' => ''
.
As to the cause of the problem i really don't know but it has something to do with Carbon
I had the same issue when using mass assignment and it turned I had forgotten to wrap my array of inputs in the request helper function
That is I had Model::create([form inputs]);
Instead of Model::create(request([form inputs]);
Just incase someone comes across it.
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