Answer the question
In order to leave comments, you need to log in
How to design seeds in laravel [best practices]?
There are Laravel Group, Students models with one-to-many relationships. I perform siding through factories and faker.
Is it ok to create related models in the same file? It is more difficult to link in different files.
<?php
use Illuminate\Database\Seeder;
use App\Group;
use App\Student;
class GroupsTableSeeder extends Seeder
{
public function run()
{
//создаем группы
factory(Group::class, 3)->create()->each(function ($g) {
//создаем студентов и сразу связываем ч-з фабрику
factory(Student::class, 10)->create([
'group_id' => $g->id
]);
});
}
}
Answer the question
In order to leave comments, you need to log in
Fine. In seeds and factories, you can safely ignore design patterns in favor of common sense - this is pretty simple code that also runs outside of production.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question