Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rhand's avatar
Level 6

Migration only adds first item of array

I am using this migration on Laravel 5.8.31 to add a table roles with three roles but somehow only the first one is added and the second two are not.

public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
        });

        DB::table('roles')->insert(
            [
                ['name' => 'administrator'],
                ['name' => 'editor'],
                ['name' => 'super-administrator'],
            ]
        );

Any ideas why this could be the case?

0 likes
18 replies
cookie_good's avatar

not an expert... stab in the dark... DB::insert likes associative arrays. It's an unattractive anomaly. Have tried ->insert(array())

???

1 like
janosk's avatar

That insert looks just fine. Do you get any error in CLI when you run the php artisan migrate command?

1 like
Snapey's avatar

Your exact code works for me. Three rows created.

1 like
rhand's avatar
Level 6

Hmm, weird. Will have to see if my local MariaDB database settings are the issue @snapey . Still getting only the first row.

@janosk the migrations just ran fine. Will also have to see if migrations after cause the two second lines / roles to be removed. Must be overlooking something here.

munazzil's avatar

Try something like below,

     DB::table('roles')->insert(
        [
            'name' => ['administrator','editor','super-administrator'],
        ]
    );
rhand's avatar
Level 6

That did not work and caused

 Illuminate\Database\QueryException  : SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into `roles` (`0`, `1`, `2`) values (administrator, editor, super-administrator))

  at /Users/me/code/site.com/valet/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
rhand's avatar
Level 6

This is weird. I just ran the migrations for the gazillionth time. And now all roles are added to my local Homebrew MariaDB database. Just weird..

munazzil's avatar

You have to add like this in your role model and then used whatever you have posted over here,because you haven't created any relationship with that,

   public function roles()
           {
              return $this->belongsToMany('App\Role');
           }
rhand's avatar
Level 6

We do have the role model Role.php:

<?php

namespace App\Models\Auth;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    public $timestamps = false;

    public function users()
    {
        return $this->hasMany(User::class);
    }
}

as well as a user model under our Models/Auth directory. In User.php we connect things between roles and the users as well using:

  /**
  * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  */
  public function role()
  {
       return $this->belongsTo(Role::class);
  }

besides other things like user groups.

Anyways, the issue is that somehow migrations do not always run properly - not all roles added to roles table - though there are no errors. Even ran php migrate -vvv but nothing useful showed up when only one role was added.

munazzil's avatar

Change hasmany() to belongsToMany()

public function users()
   {
    return $this->belongsToMany(User::class);
    }
rhand's avatar
Level 6

Yeah, @munazzil is not really helping here. He needs to read properly before he replies. Any feedback is welcome, as long as it is on point..

shez1983's avatar

stop giving rubbish advice please... his original problem has NOTHING to do with relationship - he isnt USING that to insert data.

I suggest you go back and learn laravel and its basics learn what the people are saying check your answer with someone in your team before u post an answer..

otherwise you annoy people and people will just ignore you!

1 like
temit's avatar

php artisan cache:clear and php artisan config:clear did the trick for me

Please or to participate in this conversation.