I currently have the following code in my posts factory and would like to fill the user_id field with the id of users with the role of writers in my users table.
$factory->define(Post::class, function (Faker $faker) {
return [
'title' => $faker->sentences(1, true),
'body' => $faker->sentences(3, true),
'user_id' => function() use ($faker) {
if (User::count())
return $faker->randomElement(User::pluck('id')->toArray());
else return factory(User::class)->create()->id;
},
];
});
any help on how to implement this will be appreciated.
Aucun commentaire:
Enregistrer un commentaire