The Laravel factory has a very useful for()
method. You can use it to create belongsTo()
relationships.
1public function run()2{3 Product::factory()4 ->count(3);5 ->for(Category::factory()->create())6 ->create();7}
Tip given by @mmartin_joo