Yoni Saka
Yoni Saka

Reputation: 1

how can i sum two data in two columns in different tables in laravel

I have two table order_list and menu

table order_list

table menu

i want to multiply order_list->order_qty with menu->menu_price

$order_list = Order::with('get_menu')->where('status','Completed')->orderBy('order_time','desc')->paginate(7);
$total = DB::table('order_list')
->leftjoin('menu','order_list.menu_id', '=', 'menu.menu_id')
->sum(DB::raw('order_list.order_qty * menu.menu_price'));

return view('admin.complete_order',['order_list' => $order_list],['total' => $total]);

I tried with the code above but only one data that I can retrieve

@foreach($order_list as $ol)
<tr>
    <td>{{$loop->iteration}}</td>
    <td>{{$ol->order_name}}</td>
    <td>{{$ol->get_menu->menu_name}}</td>
    <td>{{$ol->order_qty}}</td>
    <td>{{$ol->order_time->format('D H:i')}}</td>
    <td>{{$ol->get_menu->menu_price}}</td>
    <td>
        {{$total}}
    </td>
    <td class="success"><b>{{$ol->status}}</b></td>
</tr>
@endforeach

Upvotes: 0

Views: 412

Answers (1)

M Rizwan Rana
M Rizwan Rana

Reputation: 51

$salepoint = collect(DB::table('users')
        ->select('users.id', DB::raw('count(DISTINCT  sale_points.vis_id) as 
        totalinvoices'), DB::raw('sum(  sale_points.subtotal) as 
        totalsale'), DB::raw('sum(  sale_points.subtotalcost) as 
        totalcost'), DB::raw('sum(  sale_points.stotal) as 
        grandtotal'), DB::raw('sum(  sale_points.dis) as totaldiscount'))
        ->join('sale_points', 'users.id', 'sale_points.saleman_id')
        ->groupBy('users.id')
        ->get()
    );

Upvotes: 1

Related Questions