Generate multiple records specifying the values calling Factories from Tinker in Laravel

861 views Asked by At

I'm trying to figure out if it's possible to generate multiple records calling a factory with a tinker command specifying the values.
At the moment I'm generating some user Teams and Roles like this.

Team::factory()->create([
    'name' => 'Super Admin',
]);
Team::factory()->create([
    'name' => 'Admin',
]);
Team::factory()->create([
    'name' => 'Manager',
]);

--

Ability::factory()->create([
    'name' => 'Edit blog post',
]);
Ability::factory()->create([
    'name' => 'User data management',
]);

Is it possible to do it in just 2 commands instead of 5?

1

There are 1 answers

0
Davide Casiraghi On BEST ANSWER

I solved creating a seeder:

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Teams\;

class TeamSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Team::create(['name' => 'Super Admin']);
        Team::create(['name' => 'Admin']);
        Team::create(['name' => 'Manager']);
    }
}