Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first record with laravel 4 raw queries

How can i get the first record with laravel 4 raw queries.

Something like DB::select('')->first(); is not working neither did DB::first('')

like image 971
Iyad Al aqel Avatar asked Nov 05 '13 16:11

Iyad Al aqel


People also ask

Which method will retrieve the first result of the query in Laravel eloquent?

Using Laravel Eloquent you can get one row using first() method, it returns first row of table if where() condition is not found otherwise it gives the first matched row of given criteria.

What does First () return in Laravel?

It just returns null.

What is DB :: Raw in Laravel?

DB::raw() is used to make arbitrary SQL commands which aren't parsed any further by the query builder. They therefore can create a vector for attack via SQL injection.


1 Answers

The fact is that DB:select() returns an array, so you have to:

DB::select(DB::raw('select * from users'))[0];

You also can

DB::table('users')->first();
like image 188
Antonio Carlos Ribeiro Avatar answered Nov 15 '22 05:11

Antonio Carlos Ribeiro