Laravel 5.5: same field in multiple rows on Seeds

Asked

Viewed 146 times

1

As can be seen in the code below, the field created_at is the same in all, the only thing that changes is the nome. Would you have some way of writing the created_at only once, but insert in 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'),
            ]
        ]);
  • 3

    Have you thought about setting the field in the bank created_at as default for now()? this way you wouldn’t need to pass any value because the sgbd will automatically set that field as default to now

  • I didn’t mind doing that hahaha.

3 answers

2

You can create a generous array and insert the objects into one 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);
  • 1

    -1 So he will perform one insert for each iteration of foreach.

  • True @Wallacemaxters, I edited my answer.

  • 1

    Instead of array_push you could have used $generos[] = ...

1

If the Generous have a model could use the standard methods of create/save, they set this field automatically. Just like the updated_at.

Tried to put it in a variable and then just pass it to the 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]);
    }
}
  • So it works, but I wanted something that didn’t need to write created_at several times. I’ll leave the value as default even.

  • I edited the answer, if Voce has a model you can use the default functions, they set the values automatically when you call them.

  • I will do this. Another thing, the name part would not have something like this: 'name' => ['teste1', 'teste2']. Because I will always repeat the same field(name), will only change the value, I believe it would be cleaner the code.

  • I edited the answer

  • @Diegovieira Your intention is not to repeat the names of the fields, right?

1


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

If using the method insert, the insertion needs to be manual.

If you want to insert the created_at just 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.