I'm implementing a simple user role and permissions model in Laravel, but I'm not sure if the Eloquent relationship I need exists. I want to return a relationship of User's permissions, but that is via the Role model. A user only has one Role via role_id, but a Role has many Permissions.
Users belongsTo => Role hasMany => Permissions
I want an Eloquent relationship method of User->permissions() but hasManyThrough is not the right relationship structure.
Any ideas?
like this.
User
public function permissions()
{
return $this->role->permissions;
}
You can use a HasManyThrough relationship:
class User extends Model
{
public function permissions()
{
return $this->hasManyThrough(
Permission::class, Role::class, 'id', null, 'role_id'
);
}
}
I assume you have model with name User.php Role.php Permission.php
then relationship define as per below
In User.php
public function role()
{
return $this->belongsTo(Role::class, 'role_id');
}
In Role.php
public function permissions()
{
return $this->hasMany(Permission::class, 'role_id');
}
Now use this relationship
public function show($id)
{
$user = User::select('id', 'name', 'email', 'role_id')
->with([
'role.permissions'
])
->find($id);
$permissions = $user->role->permissions;
dd($permissions); //here is your permission list
}
Related
I can't find the problem in this case and need some help with Laravel Many to Many Relationship:
lluminate\Database\Eloquent\RelationNotFoundException
Call to undefined relationship [registered_events] on model [App\User].
Whenever I try to access the relation the above error shows, eventhough the relation seems correct. The inverse relation works fine.
User Model
class User extends Authenticatable
{
public function registered_events()
{
return $this->belongsToMany(Event::class, 'event_user', 'user_id', 'event_id' )->withPivot('id', 'status', 'eventmanager', 'created_at', 'updated_at');
}
}
Event Model
class Event extends Model
{
public function registered_users()
{
return $this->belongsToMany(User::class, 'event_user', 'event_id', 'user_id')->withPivot('id', 'status', 'eventmanager', 'created_at', 'updated_at');
}
}
EventAllocationController
public function admin_event_overview()
{
$user = auth()->user();
// Problem here ⬇️
$eventmanagers = $user->with('registered_events')->get();
return view('dashboard.admin.event_allocation.overview', compact('user','eventmanagers'));
}
I am trying to make 3 tables relationship like this,
users
id
name
roles
id
name
companies
id
name
company_role_user
user_id
role_id
company_id
Relationships in User.php model
public function companies() {
return $this->belongsToMany(Company::class, 'company_role_user', 'user_id', 'company_id');
}
public function roles() {
return $this->belongsToMany(Role::class, 'company_role_user', 'user_id', 'role_id');
}
public function role() {
// relationship to get role of specific company
}
Relationships in Company.php model
public function users() {
return $this->belongsToMany(User::class, 'company_role_user', 'company_id', 'user_id');
}
public function roles() {
return $this->belongsToMany(Role::class, 'company_role_user', 'company_id', 'role_id');
}
public function role() {
// relationship to get role of specific user
}
I want to get user role for specific company like this
User::find(1)->companies[0]->role->name
or
Company::find(1)->users[0]->role->name
If you have users
users
– id
– name
and you want them to have many to many relation with companies, you should do:
companies
– id
– name
user_companies
– user_id
– company_id
And for your roles, (many to many) you can do the same:
roles
- id
- name
user_roles
- user_id
- role_id
You are trying to make many to many 3 tables, when you should be doing it with 2 tables.
Even if you manage, it would be very complicated and confusing.
You should consider which tables should be related to roles, companies should have roles, or users should have roles, you dont need them both to have roles
Your schema looks correct but your model definitions/relations can use a junction/pivot model CompanyRoleUser which will relate these 3 relations as many-to-one/belongsTo and one-to-many/hasMany from Company/Role/User.
class Company extends Model
{
public function companyRoleUser()
{
return $this->hasMany(CompanyRoleUser::class, 'id', 'company_id');
}
}
class Role extends Model
{
public function companyRoleUser()
{
return $this->hasMany(CompanyRoleUser::class, 'id', 'role_id');
}
}
class User extends Model
{
public function companyRoleUser()
{
return $this->hasMany(CompanyRoleUser::class, 'id', 'user_id');
}
}
class CompanyRoleUser extends Model
{
public function company()
{
return $this->belongsTo(Discipline::class, 'id', 'company_id');
}
public function role()
{
return $this->belongsTo(Speciality::class, 'id', 'role_id');
}
public function user()
{
return $this->belongsTo(User::class ,'id','user_id');
}
}
Now you can apply different type of filters for your data like
Fetch users for company A id = 1 with admin role id = 2
User::whereHas('companyRoleUser.company', function ($query) use ($company_id) {
$query->where('id', $company_id);
})->whereHas('companyRoleUser.role', function ($query) use ($role_id) {
$query->where('id', $role_id);
});
Or
User::whereHas('companyRoleUser', function ($query) use ($company_id) {
$query->where('company_id', $company_id)
->where('role_id', $role_id);
});
Fetch companies with $user_id and $role_id
Company::whereHas('companyRoleUser', function ($query) use ($user_id) {
$query->where('user_id', $user_id)
->where('role_id', $role_id);
});
I have 3 tables users companies company_users when I try to get user company details it return null
Logic
company has many users
user belong to 1 company
user model
public function company()
{
return $this->belongsTo(Company::class, 'company_users', 'user_id', 'company_id');
}
company model
public function user()
{
return $this->belongsToMany(User::class, 'company_users', 'company_id', 'user_id');
}
any idea?
it's not one to many relation it's many to many, with pivot table (company_users )
so, the relation should be like:
public function company()
{
return $this->belongsToMany(Company::class, 'company_users', 'user_id', 'company_id');
}
I recommend rename it to 'companies' cause it plural
Regarding to this comment:
If you really want one to many relation. Try this:
public function company()
{
return $this->hasMany(User::class);
}
public function user()
{
return $this->belongsTo(Company::class);
}
I have 2 tables Users, Organization
Users hasMany Organization
Organization belongsTo User
User.php
public function organization()
{
return $this->hasMany(Organization::class, 'user_id');
}
Organization.php
public function user(){
return $this->belongsTo(Organization::class,'user_id');
}
Now I want to retrieve data of Organization with user
In Controller
dd( Organization::with('user')->get() );
but in relation, user returns null. what should I do now? Please help
In the Organization model the user relationship should be a belongsTo with User not Organization(self).
public function user()
{
return $this->belongsTo(User::class);
}
Then you can load that relationship on Organization:
$org = Organization::with('user')->get();
Hi I simply want to get permissions of the role, I am trying following
$r = Role::find(1);
dd($r->permissions);
The above script does not return any permission however you can see there is data in the below tables. I also tried following but no luck
$role = Role::with('permissions')->where('id', 1)->first();
I have data in the table as you can see
Table:tes_permissions
Table: tes_roles
Table: tes_permission_role
And following are Models
class Permission extends Model
{
protected $table = 'tes_permissions'
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
And
class Role extends Model
{
protected $table = 'tes_roles';
public function permissions() {
return $this->belongsToMany('App\Permission', 'tes_permission_role', 'permission_id', 'role_id');
}
}
Can someone kindly guide me what can be the issue, I would appreciate.
You mixed the order of properties in the belongsToMany(). The third argument is specifying the ID for the model defining the relationship. So change to the following:
public function permissions() {
return $this->belongsToMany('App\Permission', 'tes_permission_role', 'role_id', 'permission_id');
}
And on the Permission model, also define it to be sure.
public function roles()
{
return $this->belongsToMany('App\Role', 'tes_permission_role', 'permission_id', 'role_id');
}