Pluck Eager Loading Laravel 5.4

一世执手 提交于 2020-01-14 05:56:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!