Okay, let's say that I have a Post model with the attributes name, slug and content. I'd like to generate models with my ModelFactory, but want to set a specific name, which I do by overwriting the value:
factory(App\Post::class)->create(["name" => "Something here"]);
But now I want the slug to auto-generate by using the (new) name. Like
"slug" => str_slug($name);
Is this possible or do I need to write the slug manually? When using the factory below with ->create(['name' => 'anything']); the slug is not created.
My current factory
$factory->define(App\Post::class, function (Faker\Generator $faker) {
static $name;
return [
'name' => $faker->name,
'slug' => $name ?: str_slug($name),
'content' => $faker->sentences(),
];
});
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire