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

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

shareimprove this question
 
   
   
@jeewiya i'm asking specifically on how to get the FIRST RECORD like using the first() method like with Eloquent. – iyad al aqel Nov 5 '13 at 16:47
   

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();
shareimprove this answer
 
   
This is what i have right now but i was hoping for an alternative. Thanks anyway – iyad al aqel Nov 5 '13 at 16:52
   
Giving another option. But, as I said, select will return an array directly, not an object, so there are no methods. – Antonio Carlos Ribeiro Nov 5 '13 at 16:57
2 
DB::select(DB::raw('select * from users'))[0]; triggers an undefined offset exception if no records found – Agu Dondo Feb 13 '15 at 23:25
 

Like this:

DB::table('users')->take(1)->get()[0];
shareimprove this answer
 
1 
This triggers an undefined offset exception if no records found. – Gras Double Mar 15 '16 at 18:40
DB::table('users')->first(0);
shareimprove this answer
 

When using eloquent you can do this:

 $user = User::take(1)->get();
shareimprove this answer
 

来自 https://stackoverflow.com/questions/19794098/how-to-get-the-first-record-with-laravel-4-raw-queries