问题
How would I pluck an eager loading content while querying with Laravel 5.4?
I tried this way:
$something = Something::with(array('something_else' => function($query){
$query->pluck('field');
}))->first();
And $query->select('field') too, but without luck. Is this possible in Laravel 5.4?
回答1:
You wouldn't be able to use pluck on the query but you can use select if you want to limit the fields returned with eager loading.
You just need to make sure you include the id so that Eloquent can match the relationships correctly e.g.:
$something = Something::with(array('something_else' => function($query){
$query->select('id', 'field');
}))->first();
Hope this helps!
来源:https://stackoverflow.com/questions/42866017/pluck-eager-loading-laravel-5-4