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

2020-02-28 10:47发布

问题:

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