dimanche 7 juillet 2019

How to display data from controller to blade in laravel?

I have this query in controller that output data as i want. I would like to display total to the view. Right now it shows total of the first order to all orders. for example if the first order total is $56, all orders will be $56. How can i show total of every order?

This is how the controller looks like

public function viewOrders(User $user)
{
//$seller = Auth::user();
$totals = OrderProduct::select("seller_id", DB::Raw("SUM(Subtotal) AS total"), 'order_id')
->where('seller_id', '=',  \Auth::user()->id)
->groupBy('seller_id')
->groupBy('order_id')
->get();

//dd($totals);


$orders = Order::whereHas('orderItems.product', function ($query) {
    $query->where('seller_id', '=',  \Auth::user()->id);
})->get();

//dd($orders);
return view('orders', ['orders'=> $orders, 'total'=> $totals] );
}

when i dd($totals) i get

Collection {#278 ▼
#items: array:4 [▼
0 => OrderProduct {#302 ▶}
1 => OrderProduct {#303 ▶}
2 => OrderProduct {#304 ▼
  #table: "order_product"
  #fillable: array:6 [▶]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:3 [▶]
  #original: array:3 [▼
    "seller_id" => 1
    "total" => "56"------------------->I would like to show this
    "order_id" => 35
  ]
  #changes: []
}
3 => OrderProduct {#305 ▼
  #table: "order_product"
  #fillable: array:6 [▶
  #attributes: array:3 [▶]
  #original: array:3 [▼
    "seller_id" => 1
    "total" => "112"------------------->I would like to show this
    "order_id" => 36
  ]
  #changes: []
  #casts: []
}
]
}

Blade view

@foreach ($order->orderItems as $item)
@if($item->product->user_id == Auth::user()->id)

   <td></td>
   <td></td>

 @endif
 @endforeach

@foreach($total as $item)
 <td>Total: </td>
@endforeach



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire