Difference between select() and get() in laravel eloquent

我怕爱的太早我们不能终老 提交于 2019-12-18 16:30:31

问题


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


回答1:


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

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

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

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

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

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

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

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

and with get() you have to use an array

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


来源:https://stackoverflow.com/questions/27634790/difference-between-select-and-get-in-laravel-eloquent

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