Access column of target table after calling relationship - Laravel - laravel

I have three tables as follows
Base Table
users
id - integer
name - string
Target Table
roles
id - integer
name - string
Pivot Table
role_user
user_id - integer
role_id - integer
Many to many relationship is exist between users and roles table
If I want all the users with role_id of '1' so I can simply do this as
$result = User::whereHas('roles', function ($q){
$q->where('roles.id', 1);
});
but I also want the role name correspond to role_id '1' which is name column of roles table.
Is there any way I can append role name into $result collection.

I think you might want to use map(). The eager loading (with()) is very important though, to prevent the N+1 issue in your query. Without eager loading one additional query would be executed for each user in your result.
$result = App\User::query()
->with([
'roles' => function ($q) {
$q->whereId(1);
}
])
->whereHas('roles', function ($q) {
$q->whereId(1);
})
->get()
->map(function ($user) {
$user['name'] = $user->roles->first()->name;
return $user;
});

You can Eagerload the relationship to get the column you want:
User::whereHas('roles', function ($q){
$q->where('roles.id', 1);
})->with(['roles' => function ($q){
$q->select('name')->where('roles.id', 1);
}])->get();

Related

Use whereHas and query pivot table

I have 3 table: tasks, users, group, and a pivot table group_user.
A task has user_id, group_id
A group_user pivot table has user_id, group_id
I want to query the tasks if the task belongs to a group of the user.
I don't want tasks from groups that the user doesn't belong to.
What I have so far:
public function index()
{
$userId = Auth::user()->id;
return TaskResource::collection(
Task::
latest()
->whereHas('group', function($query) use($userId) {
$query->where('group_id', '=', $userId); // = is wrong
})
->get()
);
}
This gives me empty results, I tried to think about it but my head hurts
You want to get the tasks that are related to the group (or groups) of a user.
I supposed the relation name between the user table and the group table is 'users', in the Group model.
$userId = auth()->id();
$tasks = Task::latest()
->whereHas('group', function($query) use ($userId) {
$query->whereHas('users', function($query) use ($userId) {
$query->where('user_id', $userId);
});
})
->get();
Ive not tested this but i imagine you could nest the where has and use something like this in your whereHas block:
$query->whereHas('user', function($q2) use ($userId) {
$q2->where('user_id', $userId);
});

How to query count on the pivot table in Laravel eloquent

I have two data tables in database. One is user table, other one is property table. User can save as many properties as they want. Also, one property can be save by multiple users. So there is a pivot table which has the property_id and user_id in it for a record. Now I want to query the popular properties. All properties that is saved by more than 100 users are popular properties. How would I do that? Something like I am doing
public function popularProperties(Request $request)
{
$id = $request->id;
return Property::where('saved'->count(),'>',100)->with([
'paymentPlan' => function ($query){
$query->select('property_id','all_in_one_go','down_payment','minimum_installment_per_month');
},
'city',
'propertyType' => function ($query){
$query->select('id','name');
},
'user' => function ($query){
$query->select('id','name','email');
},
'images' => function($query){
$query->select('imageable_id','url');
}
])->withCount('likes')->get();
}
The following code selects all property's with > 100 count. Inside the whereIn is a subquery that selects all property IDs that exist > 100 times. Note that you have to fill in the correct Table and column names
$propertys = Property::whereIn('id', function ($q){
$q->select('property_id')
->from('user_property')
->groupBy('property_id')
->havingRaw('COUNT(*) > 100');
})->get();

Laravel 5.2 Eloquent ORM to get data from 3 tables

I have the following tables. users, user_details and client_teams. Each user has one details and each user can have many teams. schema for users:
id, name, email,parent_user_id
user_details:
id, user_id, client_team_id
client_teams:
id, user_id, team_name,status
In user_model i have the following relations:
public function userDetails(){
return $this->belongsTo('App\Models\UserDetails','id','user_id');
}
public function clientTeamList(){
return $this->hasMany('App\Models\ClientTeams','user_id','id');
}
In user_details model i have the following relation:
public function clientMemberTeam(){
return $this->belongsTo('App\Models\ClientTeams','client_team_id');
}
I want to be show the list of users who have a specific team ID and created by a specific user. The query that i am using is this:
$userCollections=Users::where([
['users.status','!=','DELETE'],
['users.parent_user_id',$clientId],
['users.id','!=',$loginUser->id]
])
->with([
'userDetails'=>function($query) {
$query->where('client_team_id',1);
}
]);
This is giving me all records for this user, Whereas i want to match by client_team_id and user_id
You need to use whereHas and orWhereHas methods to put "where" conditions on your has queries.
Please look into https://laravel.com/docs/8.x/eloquent-relationships
$userCollections = Users::where([['users.status', '!=', 'DELETE'],
['users.parent_user_id', $clientId],['users.id', '!=', $loginUser->id]
])
->whereHas('userDetails' => function ($query) {
$query->where('client_team_id', 1);
})->get();

Laravel Eloquent with() selecting specific column doesn't return results

Say I have 2 models, Category and POI where 1 Category can have many POIs.
$categoryDetails = Category::with([
'pois' => function ($query) {
$query->where('is_poi_enabled', true);
},
])->findOrFail($id);
The above query returns results from the specific Category as well as its POIs.
However, with the query below:
$query->select('id', 'name')->where('is_poi_enabled', true);
The POIs become empty in the collection.
Any idea why this is happening? When added a select clause to the Eloquent ORM?
While doing a select it's required to fetch the Relationship local or Primary key.
For an example POIs table contains category_id then it's required to select it
Try this:
$categoryDetails = Category::with([
'pois' => function ($query) {
$query->select(['id', 'category_id', 'is_poi_enabled'])
->where('is_poi_enabled', true);
},
])->findOrFail($id);
Good luck!

laravel relation issue in many to many case

i have three table in mysql:
1-users
table of users
2-projects
table of project
3-project_user
there is id and project_id and user_id for relation
there is two model : user and project
the relation between these table are belongsToMany
when a project create maybe one project define for two person
NOW how can i show the project of each person?
Assuming you properly defined your relationships, do
$users = User::with('projects')->get();
//show each user projects
foreach($users as $user) {
echo $user->projects;
}
//Getting project users
$projects = Project::with('users')->get();
This will give you projects list, in each list you can see which user has access or not.
Updates
Taking project with specified user_id
$projects = Project::with(['users' => function ($query) use ($user_id) {
$query->where('user_id', '=', $user_id);
}])->get();
New code
Or try to to get the user
$user = User::with('projects')->where('id', $user_id)->first();
Or via projects, constraining by user_id
$projects = Project::with('users')->whereHas('users', function ($query) use ($user_id) {
$query->where($user_id);
})->get();
the way i solve it:
//save user_id
$user_id = $request->id;
//select in project_user where user_id = $user_id
$project_user = DB::table('project_user')->where('user_id', '=', $user_id)->get();
//create an empty array
$p_id = array();
//fill empty array with project_id
foreach($project_user as $pro) {
$p_id[] = $pro->project_id;
}
//select code and id in project table where id = one of that [$p_id] array
$data = Project::select('code','id')->whereIn('id', $p_id)->get();
//return your data in json
return response()->json($data);
As far as I understood your question, you want to query Project as per user_id. Here is how you do it.
Project::with('users')->whereHas('users', function ($query) use ($user_id_array) {
$query->whereIn('id',$user_id_array);
})->get();
This will give you projects whose user_id are from $user_id_array. Let me know if you need any further help.

Resources