Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Seeder - multiple rows in DB

I was wondering if it's possible to insert multiple rows like this (or something like this):

<?php

use Illuminate\Database\Seeder;

class SettingTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('settings')->insert(
            [
                'key' => 'username',
                'value' => 'testusername'
            ],
            [
                'key' => 'password',
                'value' => 'plain'
            ]
        );
    }
}

I have a table settings in my database with columns key & value.

The problem with the code above is that he only inserts the first one ... .

like image 564
nielsv Avatar asked Sep 15 '15 15:09

nielsv


2 Answers

You need to wrap your arrays in another array, so it would look like this:

DB::table('settings')->insert([
    [
        'key' => 'username',
        'value' => 'testusername'
    ],
    [
        'key' => 'password',
        'value' => 'plain'
    ]
]);

Notice the wrapping array.

What you are doing now is actually sending two separate arrays to the insert() method.

like image 187
Chris Magnussen Avatar answered Nov 19 '22 19:11

Chris Magnussen


you can use insert method from eloquent for bulk save like

Settings::insert([[
            'key' => 'username',
            'value' => 'testusername'
        ],
        [
            'key' => 'password',
            'value' => 'plain'
        ]]);
like image 45
Imtiaz Pabel Avatar answered Nov 19 '22 17:11

Imtiaz Pabel