amitsolanki24_'s avatar

Seed databse with foreign key with belongsTo relationship

When I do like this it is working but I want to know more efficient way.

	#Factory
    public function definition(): array
    {
        return [
            "title" => fake()->jobTitle(),
            "type" => random_int(0, 1), // 0-> online , 1-> In person 
            "instructor_id" => Instructor::inRandomOrder()->first()->id,
            "category_id" => CourseCategory::inRandomOrder()->first()->id,
            "created_by" => User::inRandomORder()->first()->id,
            "description" => fake()->paragraph(1),
		];
}

#Seeder
Course::factory(10) ->create();

0 likes
7 replies
gych's avatar

As you said the current solution you use already works fine and you shouldn't worry about all the queries that are executed. You're just seeding the application for testing purposes.

You could also use this approach

        return [
            "title" => fake()->jobTitle(),
            "type" => random_int(0, 1), // 0-> online , 1-> In person 
            "instructor_id" => Instructor::factory(),
            "category_id" => CourseCategory::factory(),
            "created_by" => User::factory(),
            "description" => fake()->paragraph(1),
		];

Alternative Solution

An alternative solution that you could use is that if you know upfront that for example 100 users are created when seeding. You can use a random number from 1 to 100 which will match one of those user id's. This will give you the same result and can also be applied for instructor id and category id.

But if you use this 2nd approach and later decrease the amount of users that are created and forget to change this in this factory it will causes issues. So therefor it might be better to use one of the two other solutions.

1 like
rajeshtva's avatar

you can use insert() method. if you know your data upfront. like when at first you use Factory() to make courses, then at once you populate all data into your table in one query. like

$posts = Course::make(10);
Course::insert($posts->toArray());
1 like
cabsey's avatar

@amitsolanki24_ If a Course belongs to a User, you'd do:

$course->user()->associate($user);
$course->save();
1 like

Please or to participate in this conversation.