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
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!
The Laravel portal for problem solving, knowledge sharing and community building.
The community