Could you show the eloquent relationship
Get data from 3 tables with relationship and select columns
I have 3 tables
users -id -email -password and other columns...
user_details -id -user_id[FK_users] -first_name -last_name
posts -id -user_id[FK_users]
Now i already created all the relationships in the models and now i can access the user_details table by below eloquent query it returns whole users table and user_details table but i only want to select first_name and last_name from user_details table how do i do that?
$posts= Post::with('city:id,name_en', 'user.userDetail')->where('id', $id)->get();
I found answer myself
Returns whole user table and slected columns from third relationship table (user_details)
$posts = Post::with(['city:id,name_en', 'user.userDetail' => function($query)
{
$query->select('first_name', 'last_name', 'user_id');
}])->where('id', $id)->first();
Below eloquent query returns the users table id and user_details table user_id, first_name and last_name
$posts= Post::with(['city:id,name_en',
'user' => function ($query) {
$query->select('id');
},
'user.userDetail' => function($query) {
$query->select(['user_id', 'first_name', 'last_name']);
}
])->where('id', $id)->get();
Please or to participate in this conversation.