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
  • 1,988
  • 3
  • 20
  • 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
  • 83,499
  • 22
  • 207
  • 202
2

Like this:

DB::table('users')->take(1)->get()[0];
nikoskip
  • 1,809
  • 1
  • 21
  • 37
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,172
  • 1
  • 31
  • 31
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,083
  • 1
  • 18
  • 23