Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 2 years ago.
0

if you want to use $sales->total_sales with that, you'd need to replace get() with first()

$sales = DB::table('order_lines')
     ->select(DB::raw('sum(quantity*per_qty) AS total_sales'))
     ->where('product_id', '=', $product->id)
     ->first();

it's just that eloquent always returns results in a collection. there's also firstOrFail(), in case you could possibly return no row.

Last updated 2 years ago.
0

Thanks! Is there a way to just return the sum? This seems to do that and not a collection (but I can't combine columns)?

$total_sales = OrderLine::where('product_id', '=', $product->id)->sum('quantity');
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

netm netm Joined 2 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.