Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Order extends Model
- {
- public function products() {
- return $this->belongsToMany('AppProduct', 'order_products')
- ->withPivot('count', 'price', 'amount');
- }
- }
- class OrderProduct extends Model
- {
- public function product() {
- return $this->belongsTo('AppProduct');
- }
- public function order() {
- return $this->belongsTo('AppOrder');
- }
- }
- select * from orders join (
- select order_id, sum(price) * count as sum from order_products group by order_id
- ) op on orders.id = op.order_id
- $orders = Order::leftJoin('order_products', 'order_products.order_id', '=', 'orders.id')
- ->select('orders.*', DB::raw( 'sum( order_products.price * order_products.count) as `total`' ) )
- ->groupBy('orders.id')
- ->orderBy('total', 'DSC')
- ->get();
- $orders = Order::all()
- ->transform(function($item) {
- $item->total_amount = $item->products->sum('amount');
- return $item;
- });
Add Comment
Please, Sign In to add comment