R
R
raingo2018-10-10 09:50:28
Laravel
raingo, 2018-10-10 09:50:28

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

1 answer(s)
A
Alexey Ukolov, 2018-10-10
@raingo

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 question

Ask a Question

731 491 924 answers to any question