问题
I have two tables, User
and Post
. One User
can have many posts
and one post
belongs to only one user
.
In my User
model I have a hasMany
relation...
public function post(){
return $this->hasmany(\'post\');
}
And in my post
model I have a belongsTo
relation...
public function user(){
return $this->belongsTo(\'user\');
}
Now I want to join these two tables using Eloquent with()
but want specific columns from the second table. I know I can use the Query Builder but I don\'t want to.
When in the Post
model I write...
public function getAllPosts() {
return Post::with(\'user\')->get();
}
It runs the following queries...
select * from `posts`
select * from `users` where `users`.`id` in (<1>, <2>)
But what I want is...
select * from `posts`
select id,username from `users` where `users`.`id` in (<1>, <2>)
When I use...
Post::with(\'user\')->get(array(\'columns\'....));
It only returns the column from the first table. I want specific columns using with()
from the second table. How can I do that?
回答1:
Well I found the solution. It can be done one by passing a closure
function in with()
as second index of array like
Post::with(array('user'=>function($query){
$query->select('id','username');
}))->get();
It will only select id
and username
from other table. I hope this will help others.
Remember that the primary key (id in this case) needs to be the first param in the $query->select() to actually retrieve the necessary results.*
回答2:
In your Post
model
public function user()
{
return $this->belongsTo('User')->select(array('id', 'username'));
}
Original credit goes to Laravel Eager Loading - Load only specific columns
回答3:
You can do it like this since Laravel 5.5:
Post::with('user:id,username')->get();
Care for the id
field and foreign keys
as stated in the docs:
When using this feature, you should always include the id column and any relevant foreign key columns in the list of columns you wish to retrieve.
For example, if the user belongs to a team and has a team_id
as a foreign key column, then $post->user->team
is empty if you don't specifiy team_id
Post::with('user:id,username,team_id')->get();
回答4:
When going the other way (hasMany):
User::with(array('post'=>function($query){
$query->select('id','user_id');
}))->get();
Don't forget to include the foreign key (assuming it is user_id in this example) to resolve the relationship, otherwise you'll get zero results for your relation.
回答5:
In Laravel 5.7 you can call specific field like this
$users = App\Book::with('author:id,name')->get();
It is important to add foreign_key
field in the selection.
回答6:
In your Post
model:
public function userWithName()
{
return $this->belongsTo('User')->select(array('id', 'first_name', 'last_name'));
}
Now you can use $post->userWithName
回答7:
I came across this issue but with a second layer of related objects. @Awais Qarni's answer holds up with the inclusion of the appropriate foreign key in the nested select statement. Just as an id is required in the first nested select statement to reference the related model, the foreign key is required to reference the second degree of related models; in this example the Company model.
Post::with(['user' => function ($query) {
$query->select('id','company_id', 'username');
}, 'user.company' => function ($query) {
$query->select('id', 'name');
}])->get();
Additionally, if you want to select specific columns from the Post model you would need to include the user_id column in the select statement in order to reference it.
Post::with(['user' => function ($query) {
$query->select('id', 'username');
}])
->select('title', 'content', 'user_id')
->get();
回答8:
Note that if you only need one column from the table then using 'lists' is quite nice. In my case i am retrieving a user's favourite articles but i only want the article id's:
$favourites = $user->favourites->lists('id');
Returns an array of ids, eg:
Array
(
[0] => 3
[1] => 7
[2] => 8
)
回答9:
You can also specify columns on related model at the time of accessing it.
Post::first()->user()->get(['columns....']);
回答10:
Now you can use the pluck
method on a Collection
instance:
This will return only the uuid
attribute of the Post model
App\Models\User::find(2)->posts->pluck('uuid')
=> Illuminate\Support\Collection {#983
all: [
"1",
"2",
"3",
],
}
回答11:
As you set relation in model try this
$postdata = Post::with('user')->get();
$posdata = $postdata->map(function(posts){
return collection([
............
...........
'user' => $posts->user->map(function($user){
'id' => $user->id,
'username' => $user->username
})
])
})
回答12:
Try with conditions.
$id = 1;
Post::with(array('user'=>function($query) use ($id){
$query->where('id','=',$id);
$query->select('id','username');
}))->get();
回答13:
EmployeeGatePassStatus::with('user:id,name')->get();
来源:https://stackoverflow.com/questions/19852927/get-specific-columns-using-with-function-in-laravel-eloquent