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

Deekshith's avatar

how to fetch relationship data using model function

i have a model like below, User.php

public function roles()
    {
        return $this->belongsToMany('App\Models\Role')->withTimestamps();
    }

Role.php

public function permission()
    {
        return $this->hasOne('App\Models\RolePermission');
    }

Now in controller i have a query like below.

return Auth::user()->roles;

Above query will return the data correctly now i want to join permission relationsip too and if i use like below it will work,

return User::with(['roles.permission'])->where('id',auth()->user()->id)->first();

above query will return below response,

{
  "id": 1,
  "role_id": 1,
  "prefix": null,
  "first_name": "Super",
  "last_name": "Admin",
  "email": "[email protected]",
  "email_verified_at": "2021-03-17T00:00:00.000000Z",
  "avatar": null,
  "afiliation": null,
  "position": null,
  "country": null,
  "timezone": "Asia/Calcutta",
  "blocked_at": null,
  "created_at": null,
  "updated_at": "2021-04-28T08:08:23.000000Z",
  "roles": [
    {
      "id": 1,
      "name": "Admin",
      "description": "ROLE_ADMIN",
      "created_at": null,
      "updated_at": "2021-06-09T13:49:46.000000Z",
      "pivot": {
        "user_id": 1,
        "role_id": 1,
        "created_at": null,
        "updated_at": null
      },
      "permission": {
        "id": 1,
        "role_id": 1,
        "can_access_admin_panel": 1,
        "full_access": 1,
        "can_create_journal": 1,
        "can_view_journals": 1,
        "can_edit_journal": 1,
        "can_delete_journal": 1,
        "can_manage_journal": 1,
        "created_at": null,
        "updated_at": null
      }
    },
    {
      "id": 5,
      "name": "JournalAdmin",
      "description": "JOURNAL_ADMIN",
      "created_at": "2021-06-09T18:01:24.000000Z",
      "updated_at": "2021-06-09T18:01:24.000000Z",
      "pivot": {
        "user_id": 1,
        "role_id": 5,
        "created_at": null,
        "updated_at": null
      },
      "permission": {
        "id": 4,
        "role_id": 5,
        "can_access_admin_panel": 0,
        "full_access": 0,
        "can_create_journal": 0,
        "can_view_journals": 1,
        "can_edit_journal": 0,
        "can_delete_journal": 0,
        "can_manage_journal": 1,
        "created_at": null,
        "updated_at": null
      }
    }
  ]
}

But i tired to replace above code like below,

return Auth::user()->roles()->permission;

Above query returns below error,

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$permission

how to access using above method?

0 likes
2 replies
SilenceBringer's avatar
Level 55

@deekshith if you need the list of permissions, you can get it like this:

return Permission::whereHas('roles.users', function ($query) {
    $query->where('id', Auth::id());
})->get();

Please or to participate in this conversation.