php - Laravel 5 - How to count total records with joins and group by -


i need count total no. of records coming database group by. don't need count of each set or group by default.

one way know use ->get() on query, crashes when have lots of record in database.

        $orders = order::where('orders.store_id', $store->id);         $orders->join('order_product', 'orders.id', '=', 'order_product.order_id');         $orders->join('products', 'products.id', '=', 'order_product.product_id');         $orders->join('customers', 'order_product.order_id', '=', 'customers.order_id');         $orders->join('addresses', 'customers.id', '=', 'addresses.customer_id');         $orders->where('products.status', 1);         $orders->where('orders.is_deleted', '0');          if ($keyword) {             $orders->where(function ($query) use ($keyword, $searchkeyword){                 $query->where('products.title', 'like', $searchkeyword)                       ->orwhere('orders.order_name', 'like', $searchkeyword);                  if (strtolower($keyword) == 'enabled') {                     $query->orwhere('orders.status', '=', 1);                 }elseif (strtolower($keyword) == 'disabled') {                     $query->orwhere('orders.status', '=', 0);                 }                  return $query;             });         }          $orders->groupby('orders.id');          // total orders         $totalorders = count($orders->get());          $orders->orderby($orderby, $orderdirection)->skip($startfrom)->take($itemsperpage);         $orders = $orders->select([             'orders.id',             'orders.order_name',             'products.title',             'products.handle',             'products.id product_id',             'orders.status'         ])->get(); 

copy comments:

i assume want paginate $orders.

$orders->paginate($itemsperpage) 

gives exact same results as

$orders->skip($startfrom)->take($itemsperpage)->get(). 

there's slight difference, because model::get() gives collection instance, while model::paginate() returns lengthawarepaginator. can iterate on both.
please see pagination on laravel docs.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -