Laravel - Seeding Relationships

前端 未结 6 507
感情败类
感情败类 2020-12-07 14:58

In Laravel, database seeding is generally accomplished through Model factories. So you define a blueprint for your Model using Faker data, and say how many instances you ne

6条回答
  •  醉梦人生
    2020-12-07 15:53

    this worked for me in laravel v8

    for ($i=0; $i<=2; $i++) {
        $user = \App\Models\User::factory(1)->create()->first();
        $product = \App\Models\Product::factory(1)->create(['user_id' => $user->id])->first();
    }
    

提交回复
热议问题