Pluck渴望加载Laravel 5.4 [英] Pluck Eager Loading Laravel 5.4

查看:66
本文介绍了Pluck渴望加载Laravel 5.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 5.4中进行查询时,我如何渴望加载内容?

How would I pluck an eager loading content while querying with Laravel 5.4?

我尝试过这种方式:

$something = Something::with(array('something_else' => function($query){
    $query->pluck('field');
}))->first();

$query->select('field')也一样,但是没有运气.在Laravel 5.4中有可能吗?

And $query->select('field') too, but without luck. Is this possible in Laravel 5.4?

推荐答案

您将无法在查询中使用pluck,但是如果您想限制急于加载的返回字段,则可以使用select.

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.

您只需要确保包含id,以便Eloquent可以正确匹配关系,例如:

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();

希望这会有所帮助!

这篇关于Pluck渴望加载Laravel 5.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆