10

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

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

Iyad Al aqel
  • 2,020
  • 3
  • 21
  • 32

5 Answers5

27

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();
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
2

Like this:

DB::table('users')->take(1)->get()[0];
nikoskip
  • 1,860
  • 1
  • 21
  • 38
1
DB::table('users')->first(0);
undefined
  • 161
  • 3
  • 16
1

When using eloquent you can do this:

 $user = User::take(1)->get();
Cengkuru Michael
  • 4,590
  • 1
  • 33
  • 33
0

Use like this

$allmarketers = ProductStock::take(3)->get()[2]; 

Take will return how many rows you want and you use the offset like 2 to pick individual rows

Pantani
  • 1,223
  • 1
  • 18
  • 25