⬆️ Go to main menu ⬅️ Previous (Artisan) ➡️ Next (Log and debug)
- Factory callbacks
- Generate Images with Seeds/Factories
- Override values and apply custom login to them
- Using factories with relationships
- Create models without dispatching any events
- Useful for() method
- Run factories without dispatching events
- Specify dependencies in the run() method
While using factories for seeding data, you can provide Factory Callback functions to perform some action after record is inserted.
$factory->afterCreating(App\User::class, function ($user, $faker) {
$user->accounts()->save(factory(App\Account::class)->make());
});
Did you know that Faker can generate not only text values but also IMAGES? See avatar
field here - it will generate 50x50 image:
$factory->define(User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => bcrypt('password'),
'remember_token' => Str::random(10),
'avatar' => $faker->image(storage_path('images'), 50, 50)
];
});
When creating records with Factories, you can use Sequence class to override some values and apply custom logic to them.
$users = User::factory()
->count(10)
->state(new Sequence(
['admin' => 'Y'],
['admin' => 'N'],
))
->create();
When using factories with relationships, Laravel also provides magic methods.
// magic factory relationship methods
User::factory()->hasPosts(3)->create();
// instead of
User::factory()->has(Post::factory()->count(3))->create();
Tip given by @oliverds_
Sometimes you may wish to update
a given model without dispatching any events. You may accomplish this using the updateQuietly
method
Post::factory()->createOneQuietly();
Post::factory()->count(3)->createQuietly();
Post::factory()->createManyQuietly([
['message' => 'A new comment'],
['message' => 'Another new comment'],
]);
The Laravel factory has a very useful for()
method. You can use it to create belongsTo()
relationships.
public function run()
{
Product::factory()
->count(3);
->for(Category::factory()->create())
->create();
}
Tip given by @mmartin_joo
If you want to create multiple records using Factory without firing any Events, you can wrap your code inside a withoutEvents closure.
$posts = Post::withoutEvents(function () {
return Post::factory()->count(50)->create();
});
Tip given by @TheLaravelDev
You can specify dependencies in the run()
method of your seeder.
class DatabaseSeeder extends Seeder
{
public function run()
{
$user = User::factory()->create();
$this->callWith(EventSeeder::class, [
'user' => $user
]);
}
}
class EventSeeder extends Seeder
{
public function run(User $user)
{
Event::factory()
->when($user, fn($f) => $f->for('user'))
->for(Program::factory())
->create();
}
}
Tip given by @justsanjit