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

Orgil's avatar
Level 2

Laravel multiple table

Hello there!, I have following 3 tables 1. User 2.Profile 3. Permission The question is how can i get result from these tables ? I want to check user permission in View For ex: User has a profile which is Admin, admin have permission according to permission table which means configure permission dynamically

User Model

<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
    public function profiles()
        {
            return $this->belongsTo('App\Profile');
        }
}

Profile

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    public function users()
    {
        return $this->hasMany('App\User');
    }

    public function permissions()
    {
      return $this->hasMany('App\Permission');
    }
}

Permission

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Permission extends Model
{
    public function profiles()
    {
      return $this->belongsTo('App\Profile');
    }
}
0 likes
2 replies
jlrdw's avatar

You know you could have the roles in the users table as well.

vitr's avatar

I think, you don't need to know which profile is assigned to a user, what you really need is to verify a particular permission. I would write a method in User class, note, it should be singular profile as you use belongsTo (one to many). First, you get the profile assigned to a user, then get the collection of permissions and search for the requested permission, if it's not found you return false - not allowed, otherwise - true.

public function allowed(Permission $permission)
{
    return $this->profile->permissions->search($permission) !== false;
}

or you could use it inline, it just looks too long .

Please or to participate in this conversation.