Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 717
Rajesh kumawat Avatar asked Dec 24 '14 09:12

Rajesh kumawat


Video Answer


1 Answers

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'))
like image 52
lukasgeiter Avatar answered Sep 17 '22 15:09

lukasgeiter