Laravel DB insert([]) only insert first record of array

Viewed 1653

I have this simple seeder class but for some reason is only inserting the first element from the array and the command is not returning any error.

Im using Laravel 5.8 and postgres as DB engine.

<?php

use Illuminate\Support\Carbon;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class PermissionsTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('permissions')->insert([
            'id' => 1,
            'name' => 'list_users',
            'display_name' => 'List users',
            'description' => 'Can list all users',
            'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
            'updated_at' => Carbon::now()->format('Y-m-d H:i:s')
        ],
        [
            'id' => 2,
            'name' => 'show_user',
            'display_name' => 'Show single user',
            'description' => 'Can show single user details',
            'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
            'updated_at' => Carbon::now()->format('Y-m-d H:i:s')
        ]);  
    }
}

Result:

enter image description here

1 Answers

You need to provide an array of arrays as the first argument:

DB::table('permissions')->insert([
    [
        'id' => 1,
        'name' => 'list_users',
        'display_name' => 'List users',
        'description' => 'Can list all users',
        'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
        'updated_at' => Carbon::now()->format('Y-m-d H:i:s')
    ],
    [
        'id' => 2,
        'name' => 'show_user',
        'display_name' => 'Show single user',
        'description' => 'Can show single user details',
        'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
        'updated_at' => Carbon::now()->format('Y-m-d H:i:s')
    ]
]);
Related