I have a query
$result = DB::select("SELECT * FROM table_name WHERE id=$id");
I want to get one object, but it returns an array of objects.
I added ->first()
to my code as follows:
$result = DB::select("SELECT * FROM table_name WHERE id=$id")->first();
, and I got an error:
Call to a member function first() on array
How to get one object via DB
?
2
Answers
Change it to use the query builder instead of the select statement
DB::select method returns an array, and you are trying to call the first() method on an array which is not a valid operation. What that means is $result is an array. You can try this below which will filter based on one specific id in a safer way.