mardi 11 décembre 2018

Laravel - Populating old values from pivot table within iteration that contains null values

I currently have a Many to Many relationship between Order and Product. The pivot table contains an additional field qty.

I show the form for creating an Order and saving data to the pivot as such:

@foreach ($products as $product)
  
  
  
@endforeach

From here, my controller saves the Order, filters through the products and only attaches the products which are not-null into the pivot table.

I am then able to view the same Order using:

{!! Form::model($order, ['route' => 'order.update', $order->id]]) !!}
  @foreach ($order->products as $product)
    
    
  @endforeach

However, this obviously does not show all available products, only the products that have been given a qty.

This is my Controller:

public function edit(Order $order)
{
 $user = Auth::user();
 $user->load("orders.products"); //eager load pivot table
 $products = Product::get();

 return view('orders.edit', compact('order', 'products', 'user'));
}

How can I populate all Products into the view while still bringing in the qty info?

I believe I am implementing Form-Model-Binding correctly, but I am still not sure how access the pivot data at the same time I am iterating through the products



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire