Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A four digit year could not be found Data missing

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???

like image 592
Juliatzin Avatar asked Jan 23 '16 16:01

Juliatzin


3 Answers

have you tried using key value array in the create method:

factory(User::class)->create(['role_id' => 2]);

like image 178
jakehallas Avatar answered Sep 27 '22 23:09

jakehallas


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

like image 45
James Dube Avatar answered Sep 28 '22 00:09

James Dube


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.

like image 33
F KIng Avatar answered Sep 28 '22 01:09

F KIng