Md Rashedul Hoque Bhuiyan
Md Rashedul Hoque Bhuiyan

Reputation: 10641

Illegal offset type while creating new record in laravel 5.3

I am trying to create new record using seeder.

This is the code i wrote on seeder file:

RoleUser::create(['role_id'=>$roleId[0],'user_id'=>$user->id]);
PermissionRole::create(['permission_id'=>1,'role_id'=>$roleId[0]]);

This is the error i am getting

[ErrorException] Illegal offset type 

RoleUser model is used for role_user table

Schema::create('role_user', function (Blueprint $table) {
        $table->integer('role_id')->unsigned();
        $table->integer('user_id')->unsigned();
        $table->primary(['role_id','user_id']);
    });

PermissionRole model is used for permission_role table

Schema::create('permission_role', function (Blueprint $table) {
        $table->integer('permission_id')->unsigned();
        $table->integer('role_id')->unsigned();
        $table->primary(['permission_id','role_id']);
    });

RoleUser Model has following code :

    class RoleUser extends Model
{
    protected $table='role_user';
    protected $primaryKey=['role_id','user_id'];
    protected $fillable=['role_id','user_id'];
    public $timestamps=false;
}

Upvotes: 0

Views: 315

Answers (2)

Md Rashedul Hoque Bhuiyan
Md Rashedul Hoque Bhuiyan

Reputation: 10641

Finally figured it out. The problem occurred because i used following code in RoleUser model :

protected $primaryKey=['role_id','user_id'];

Removed this line and now its working fine.

Upvotes: 1

Amit Gupta
Amit Gupta

Reputation: 17688

Illegal offset type errors occur when you attempt to access an array index using an object or an array as the index key.

Probably the error should be how you are getting $roleId and $user.

You'll have to make sure $roleId and $user contains what you want it to and that you're accessing it correctly.

Upvotes: 0

Related Questions