Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

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.

0

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

Last updated 7 years ago.
0

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

1

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

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)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

nasik21 nasik21 Joined 24 Mar 2016

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.