0

I am trying to override a view of voyager-admin and edit it but I get an "Undefined variable: products" error and I can't figure out why. Here is my OrdersController:

$order = Orders::find($id);
$products = $order->products;

return Voyager::view($view, compact('dataType', 'dataTypeContent', 'isModelTranslatable','order', 'products'))

Here is my view:

<ul>
    @foreach ($products as $product)
    <li style="margin-bottom: 10px">
    <div>ID Produs: {{$product->id}}</div>
    <div>Nume Produs: {{$product->name}}</div>
    <div>Pret Produs: {{$product->presentPrice()}}</div>
    <div>Cantitate: {{$product->pivot->quantity}}</div>
    </li>
    @endforeach
 </ul>

If anything more needed please let me know, I just can't understand why is throwing me this error.

My Orders Model

 public function products(){
  return $this->belongsToMany('App\Product')->withPivot('quantity');
}

My Products Model

 public function orders(){

    return $this->belongsToMany('App\Orders');
}

2 Answers2

0

Add validation to check your collectio

$order = Orders::find($id);
if($order != null){
$products = $order->products;

return Voyager::view($view, compact('dataType', 'dataTypeContent', 'isModelTranslatable','order', 'products'))
} else {
return null;
}
0

I forgot to override the controller in the Voyager-Admin.

double-beep
  • 5,031
  • 17
  • 33
  • 41