Support the ongoing development of →
Database Eloquent
Last updated 2 years ago.

DB::select() and DB::get() will give an array or a collection (in the latest Laravel)

You can call ->first() to get the first item.


->first() gives error Call to a member function first() on array

Last updated 7 years ago.

you must be using Laravel 5.2 or earlier.

You can do this.

collect(\DB::select("SELECT * FROM table_name WHERE id=$id"))->first();

ifarooq liked this reply


Why not use Query Builder and do something like this:

$user = DB::table('users')->where('id', $id)->get();

If you need to get specific columns:

$user = DB::table('users')->where('id', $id)->pluck('id', 'name');

nasik21 said:

->first() gives error Call to a member function first() on array

You should call ->first() "in place" of ->get() or select() , not after. I need to hint though, that ->first() returns the row as an object (rather than array)


Sign in to participate in this thread!


Your banner here too?

nasik21 nasik21 Joined 24 Mar 2016


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.