Accessing related models in many to many relationships in Laravel - php

I have three tables users, profiles and roles. The users and profiles tables has one to one relationships and the users and roles has many to many relationships using a pivto table role_user. In the Role model, I retrieve related users:
Role model
public function users()
{
return $this->belongsToMany(User::class);
}
My problem is that the above method only retrieves the users and not their profiles. In fact, I want to get users as well as their profiles using a role model and then paginate them.
public function getUsers($role)
{
return $role->users()->paginate(50);
}
So, how can I retrieve users and their profiles using a Role model?

In your User model you define your Profile relationship:
public function profile()
{
return $this->hasOne(Profile::class);
}
and then when you call the getUsers() method you can eagerload the users and their profile:
$role = Role::with('users.profile')->find($id);
$result = getUsers($role);

Related

How do I access one model from another which don't have direct relationship?

I have an User model. Each user has certain grades and each grade has certain students.
User and Grade models have one-to-many relationship. Grade and Student models have one-to-many relationship too. But, User and Student models don't have any relationship.
Once User is logged in, he should be able to search all students that belongs to the user.
Do I have to create relationship between these two models to?
If I understand the question correctly, a user has multiple students, so what I'd do is create a one-to-many relationship between them like this:
//User.php
public function students()
{
return $this->hasMany(Student::class);
}
//Student.php
public function user()
{
return $this->belongsToOne(User::class);
}
This would allow you to pick up a users students by doing: $students = $user->students Note: I didn't use () here. If you were to execute $students = $user->students() you'd end up with a query builder object.

Laravel eloquent many to many relationship determine if relation exist

I am working on a project with many-to-many relationship between two tables roles and users. I also have a pivot table role_user that holds information about the relation between the other two tables.
role_user two fields namely the ids of the two tables: role_id and user_id.
Now I have a user object gotten from the users table and another role object gotten from the roles table. I want to determine if a particular role belongs to a certain user.
I know this can be done by creating a model for the pivot table and then using the model, the role_user table can be queried and determined if the relation exists.
But I find this method stressful and wonder if eloquent provides an easy method to determine this.
Thanks for any help?
You don't need to have a role_user model. You might already have role model and the user model related to the tables you have.
Specify the many to many relationship in Role.php and User.php
User.php - function roles
public function roles()
{
return $this->belongsToMany('App\Role');
}
Role.php - function users
public function users()
{
return $this->belongsToMany('App\User');
}
You may have to define other parameters like pivot table etc if you don't follow the naming conventions in table columns and tables. Since you seem to have follow the rules I'm not going to explain it. But you always can refer to laravel documentation's eloquent many-to-may section
now you can easily check whether a user belongs to a certain role.
Consider the following examle
//select a user
$user = App\User::find(1);
//select a role
$role = App\Role::find(1);
//get $user's roles. This ill return an array of role objects that are belong to $user
$userRoles = $user->roles;
//check whether $role is in that array
if(in_array($role, $userRoles)){
//do something
}

How to establish a hasManyThrough relationship in laravel for roles and permissions?

So I understand how to load the roles for a user and the permissions for a role.
But now I have a user table, a role table, and a permission table. I also have role_user table for linking users and roles. And of course a permission_role for linking permissions and roles.
Now when I want to get all the roles for the user, I simply do something like this:
public function roles()
{
return $this->belongsToMany('Uppdragshuset\AO\Tenant\Models\Role');
}
Similarly I can fetch permissions for roles like so:
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
Now according to the documentation on laravel, I can directly fetch permissions for a user by using the hasManyThrough relation too like so:
public function permissions()
{
return $this->hasManyThrough(Permission::class, Role::class);
}
But this is returning with an error saying:
Unknown column 'role.user_id' in 'field list'
I think I understand why. Laravel is looking for user_id field in the role table but it does not understand that it is a many to many relation and it should look for it in the pivot table.
So what is the way around this? Is there a way around this in Eloquent or will I have to resort to using the query builder? And if yes, how to do the same thing with the query builder?
HasManyThrough can only be used to connect two HasMany relationships. There is no native relationship for your case.
I created a HasManyThrough relationship with unlimited levels and support for BelongsToMany:
Repository on GitHub
After the installation, you can use it like this:
class User extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function permissions() {
return $this->hasManyDeep(Permission::class, ['role_user', Role::class, 'permission_role']);
}
}

Laravel eloquent get specific user with his relations

Hello i am trying to get one user with his relationships. So one user form user table could have many items, so relationship is one to many.
Booth model
public function booths() {
return $this->hasMany('App\Booth');
}
User model
public function users() {
return $this->belongsTo('App\User');
}
I set foreign key from Booths table user_id is connected with Id from User table.
I try this query
$booth =User::findOrFail(83)->booths()->get();
But get all users. Also tried with with() but in that case i get all booths.
Change your code to
$user = User::with('booths')->findOrFail(83);

Laravel Relationship Between Tables

I have generate the following database table and need to create model form them.
Creating a model seems to be an easy task, but I am confused how should i define the relationship between two tables. In users table RoleId is foreign key. My question is where and how should I define the relationship (in User model or Role model). Should I use hasOne, hasMany, or belongsTo
In your User model
public function roles()
{
return $this->hasOne('Role', 'id', 'RoleId');
}
In your Role model
public function users()
{
return $this->belongsTo('User', 'RoleId', 'id);
}

Categories