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

后端 未结 1 348
独厮守ぢ
独厮守ぢ 2020-12-31 18:26

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

相关标签:
1条回答
  • 2020-12-31 19:09

    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'))
    
    0 讨论(0)
提交回复
热议问题