1

I am using Query Builder and here is my code in Laravel5

$users = DB::table('brand_customers')
            ->select('brand_id')
            ->where('mobile', $mobile)
            ->take(15)
            ->get();

I am limiting to 15 records but I am also looking for total count. Currently, I am getting count using this.

$users->count();

I tried the above code but no luck. How can I get both the data and count with the same query. Thanks.

Chopra
  • 571
  • 4
  • 8
  • 23
  • replace the get() with count() and you get the count, if you want both, get the users (like you're doing) and just count($users) – Edd Turtle Apr 03 '15 at 12:21
  • If I want to limit 15 records and also want to get total counts, then how can i do that? – Chopra Apr 03 '15 at 12:26
  • ... two separate statements? I think is the way – Edd Turtle Apr 03 '15 at 12:27
  • I think, this can be possible with count(*) as total with DB::raw. It was possible in CodeIgniter. – Chopra Apr 03 '15 at 12:30
  • 1
    http://laravel.com/docs/5.0/queries#raw-expressions – hakre Apr 03 '15 at 12:32
  • Here is the answer to this question. [http://stackoverflow.com/questions/13223512/how-to-select-count-with-laravels-fluent-query-builder][1] [1]: http://stackoverflow.com/questions/13223512/how-to-select-count-with-laravels-fluent-query-builder – Chopra Apr 03 '15 at 15:53

1 Answers1

0

Use two separate statements like:

$users = DB::table('brand_customers')
        ->select('brand_id')
        ->where('mobile', $mobile);

$count = $users->count();
$users = $users->take(15)->get();
Raviraj Chauhan
  • 655
  • 5
  • 7