mysql - Laravel - get total count of rows in group by -


i have following code in 1 of laravel controllers:

$stock_items = db::table('stock_items')         ->leftjoin('stock_types', 'stock_items.stock_type_id', '=', 'stock_types.id')         ->select('stock_types.name', db::raw('sum(stock_items.weight) total_weight'), db::raw('sum((stock_items.weight / 1000) * stock_items.price_per_ton) total_price'))         ->groupby('stock_types.name')         ->get();     return view('dashboard', ['stock_items' => $stock_items, 'stock_type']); 

the above works fine , can access results in blade template. however, i need count of rows each groupby clause. tried amending above to:

$stock_items = db::table('stock_items')         ->leftjoin('stock_types', 'stock_items.stock_type_id', '=', 'stock_types.id')         ->select('stock_types.name', db::raw('sum(stock_items.weight) total_weight'), db::raw('sum((stock_items.weight / 1000) * stock_items.price_per_ton) total_price'), db::raw(count(*) total_rows))         ->groupby('stock_types.name')         ->get();     return view('dashboard', ['stock_items' => $stock_items, 'stock_type']); 

but returns 1 each time, when know not correct.

i appreciate if can shed light on please.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -