Laravel 5.5: same field in several rows in seeds

1

As you can see in the code below, the created_at field is the same at all, the only thing that changes is nome . Would you have any way to write created_at only once, but insert it on all lines?

DB::table('generos')->insert([
            [
                'nome' => 'Ação',
                'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
            ],
            [
                'nome' => 'Comédia',
                'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
            ],
            [
                'nome' => 'Terror',
                'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
            ],
            [
                'nome' => 'Policial',
                'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
            ],
            [
                'nome' => 'Fantasia',
                'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
            ]
        ]);
    
asked by anonymous 16.11.2017 / 13:49

3 answers

1

By default in Laravel created_at is inserted automatically, except in cases where you are using a Model, of course.

If you use the insert method, the insertion must be done manually.

If you want to enter created_at only once as commented, just refactoring the code completely:

$now = new DateTime;

$generos = array_map(function ($genero) use ($now) {

    return ['nome' => $genero, 'created_at' => $now];
}, [
    'Ação',
    'Comédia',
    'Fantasia',
    'Policial',
    'Terror',
]);

DB::table('generos')->insert($generos);
    
16.11.2017 / 14:14
2

You can create an array of genres and insert the objects into a foreach

$nomes = array('Ação', 'Comédia', 'Terror');
$generos = [];
$tabela = DB::table('generos')
foreach ($generos as $genero) {
    array_push($generos, ['nome' => $genero, 'created_at' => Carbon::now()]);
}
$tabela->insert($generos);
    
16.11.2017 / 14:14
1

If Generos has a model you could use the default methods of create/save , they set that field automatically. Just like updated_at .

Tried to put in a variable and then just pass it to insert ?

$now = Carbon::now()->format('Y-m-d H:i:s');
DB::table('generos')->insert([
    [
        'nome' => 'Ação',
        'created_at' => $now,
    ],
    [
        'nome' => 'Comédia',
        'created_at' => $now,
    ],
    [
        'nome' => 'Terror',
        'created_at' => $now,
    ],
    [
        'nome' => 'Policial',
        'created_at' => $now,
    ],
    [
        'nome' => 'Fantasia',
        'created_at' => $now,
    ]
]);

With an array of names and a foreach to save them: before opening the class you add the:

use App\Genero;

After opening it:

public $genero;
public function __construct(Genero $genero){
    $this->genero = $genero;
}

function xyz(){
    $nomes = array('Ação', 'Comédia', 'Terror', 'Policial', 'Fantasia');
    foreach($nomes as $nome){
        $genero = App\Genero::create(['nome' => $nome]); // Se estiver definido no construct vai usar o this:
        $genero = $this->genero->create(['nome' => $nome]);
    }
}
    
16.11.2017 / 13:52