Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 808
Ricardo Albear Avatar asked Jan 25 '26 15:01

Ricardo Albear


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')
    ]
]);
like image 194
Jonas Staudenmeir Avatar answered Jan 29 '26 13:01

Jonas Staudenmeir