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

jrdavidson's avatar

@thomaskim

Below is what shows when I dd($role) I get this... (which are the correct roles that have access to the page.

See additional dd below.

Collection {#168 ▼
  #items: array:2 [▼
    0 => Role {#154 ▼
      #connection: null
      #table: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:6 [▶]
      #original: array:8 [▼
        "id" => 4
        "name" => "owner"
        "label" => "Site Owner"
        "created_at" => "2015-09-14 13:51:45"
        "updated_at" => "2015-09-14 13:51:45"
        "deleted_at" => null
        "pivot_permission_id" => 1
        "pivot_role_id" => 4
      ]
      #relations: array:1 [▶]
      #hidden: []
      #visible: []
      #appends: []
      #fillable: []
      #guarded: array:1 [▶]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
    1 => Role {#161 ▼
      #connection: null
      #table: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:6 [▶]
      #original: array:8 [▼
        "id" => 5
        "name" => "webmaster"
        "label" => "Webmaster"
        "created_at" => "2015-09-14 13:51:45"
        "updated_at" => "2015-09-14 13:51:45"
        "deleted_at" => null
        "pivot_permission_id" => 1
        "pivot_role_id" => 5
      ]
      #relations: array:1 [▶]
      #hidden: []
      #visible: []
      #appends: []
      #fillable: []
      #guarded: array:1 [▶]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
  ]
}

If I dd($this->role) this is what I get... (not sure how this happened.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles.user_id' in 'where clause' (SQL: select * from `roles` where `roles`.`user_id` = 3 and `roles`.`user_id` is not null limit 1)
jrdavidson's avatar
jrdavidson
OP
Best Answer
Level 54

I figured it out I needed to do this is my trait...

public function role()
    {
        return $this->belongsTo(Role::class);
    }

And this in my role model.

public function users()
    {
        return $this->hasMany(User::class);
    }
1 like
thomaskim's avatar

@xtremer360 Ohhhh. Now things are starting to make sense.

If you setup a hasOne / belongsTo relationship, that means that a user has one role. That also means a role can only belong to one person. In other words, there is only one Site Owner, one Webmaster, etc. If this is what you want, then your Role's model needs to have a user_id. Right now, your user has a role_id.

If that is not what you want, then I'm guessing what you want is that a role hasMany users while a user belongsTo a role. This relationship means that a role can be assigned to multiple people. For example, you can have multiple Webmasters, but each person can only belong to a single role.

jrdavidson's avatar

The second one was correct. I was able to correct the issue before your last reply. Check out how it was accomplished.

Previous

Please or to participate in this conversation.