@markdown# Factory callbacksWhile using factories for seeding data, you can provide Factory Callback functions to perform some action after record is inserted.```php$factory->afterCreating(App\User::class, function ($user, $faker) { $user->accounts()->save(factory(App\Account::class)->make());});```### Generate Images with Seeds/FactoriesDid you know that Faker can generate not only text values but also IMAGES? See `avatar` field here - it will generate 50x50 image:```php$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) ];});```### Override values and apply custom login to themWhen creating records with Factories, you can use Sequence class to override some values and apply custom logic to them.```php$users = User::factory() ->count(10) ->state(new Sequence( ['admin' => 'Y'], ['admin' => 'N'], )) ->create();```### Using factories with relationshipsWhen using factories with relationships, Laravel also provides magic methods.```php// magic factory relationship methodsUser::factory()->hasPosts(3)->create();// instead ofUser::factory()->has(Post::factory()->count(3))->create();```Tip given by [@oliverds_](https://twitter.com/oliverds_/status/1441447356323430402)### Create models without dispatching any eventsSometimes you may wish to `update` a given model without dispatching any events. You may accomplish this using the `updateQuietly` method```phpPost::factory()->createOneQuietly();Post::factory()->count(3)->createQuietly();Post::factory()->createManyQuietly([ ['message' => 'A new comment'], ['message' => 'Another new comment'],]);```### Useful for() methodThe Laravel factory has a very useful `for()` method. You can use it to create `belongsTo()` relationships.```phppublic function run(){ Product::factory() ->count(3); ->for(Category::factory()->create()) ->create(); }```Tip given by [@mmartin_joo](https://twitter.com/mmartin_joo/status/1461002439629361158) ### Run factories without dispatching eventsIf you want to create multiple records using Factory without firing any Events, you can wrap your code inside a withoutEvents closure.```php$posts = Post::withoutEvents(function () { return Post::factory()->count(50)->create();});```Tip given by [@TheLaravelDev](https://twitter.com/TheLaravelDev/status/1510965402666676227)### Specify dependencies in the run() methodYou can specify dependencies in the `run()` method of your seeder.```phpclass DatabaseSeeder extends Seeder{ public function run() { $user = User::factory()->create(); $this->callWith(EventSeeder::class, [ 'user' => $user ]); }}``````phpclass 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](https://twitter.com/justsanjit/status/1514428294418079746)@endmarkdown
We use cookies to improve your experience and to help us understand how you use our site. By using this site, you accept our use of cookies. Cookie Info x