samedi 8 décembre 2018

Laravel - Populating view with info from single row (with pivot data)

I currently have a form/view that allows a User to save an Order. This also updates a pivot table with the respective Products and qtys. What I am trying to do, is after this information is inputted and saved into the database, I want to send the user to a checkout page that shows all the information they inputted for that Order.

Relationships

User hasMany Order

Order belongsToMany Product

Product belongsToMany Order

checkout.blade.php

@foreach ($orders as $order)
  @foreach ($order->products as $product)
    <tr>
    <td></td>
    <td></td>
  </tr>
    @endforeach
@endforeach

OrderController.php

public function checkout($id)
{
    $user = Auth::user();
    $user->load("orders.products"); //eager load pivot table
    $orders = $user->orders->where('order_id', $id);

    return view('orders.checkout', compact('orders', 'user'));
}

I know I am calling the order incorrectly, because if I change

    $orders = $user->orders->where('order_id', $id);

to

    $orders = $user->orders;

It displays correctly, except of course, it populates with every Order's detail.

Or is there some elegant way for me to pass the data from the checkout function without this additional query? (I understand about moving data in Sessions, but I am working with a pivot table, and that complicates things.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire