laravel eloquent 中 select() 和 get() 的区别 [英] Difference between select() and get() in laravel eloquent

查看:24
本文介绍了laravel eloquent 中 select() 和 get() 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 laravel eloquent 模型时,get() 和 select() 方法有什么区别.哪种方法更快?

Is there any Difference between get() and select() method when using laravel eloquent model. Which method is faster?

推荐答案

是的,有区别.select() 仅用于定义您想要的列.get() 用于实际获取结果(> 执行查询),它还允许您指定列.

Yes there is a difference. select() is only for defining which columns you want. get() is for actually getting the result (> executing the query) and it also allows you to specify columns.

DB::table('foo')->select(array('bar'));

不会不执行任何东西.你仍然需要 get()

Will not execute anything. You still need get() for that

DB::table('foo')->select(array('bar'))->get();

现在您收到的结果只有 bar 列.
也可以这样做:

Now you receive a result with only the bar column.
The same can be done this way:

DB::table('foo')->get(array('bar'));

所以语法方面单独get()更快(意味着更短),而性能方面你不会注意到任何差异.

So syntax-wise get() alone is faster (meaning shorter) while performance wise you won't notice any difference.

另一个小区别:通过 select() 你可以使用列表语法

Another little difference: with select() you can use the list syntax

select('foo', 'bar', 'etc', 'whatever')

并且使用 get() 你必须使用一个数组

and with get() you have to use an array

get(array('foo', 'bar', 'etc', 'whatever'))

这篇关于laravel eloquent 中 select() 和 get() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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