vendredi 5 juillet 2019

Loop orders in Laravel eloquent

I have three tables order_product, users and orders table, they all related each other and they work fine(as expected). I want to loop orders and not OrderProduct and also display them in the view . So far it is looping OrderProduct instead of Order, how can I loop orders and display in the view?

Here are the code.

Blade view

    @foreach ($orders as $order)
    <div></div>
    @endforeach

    @foreach ($order->productInfo as $product)
   <div></div>
    @endforeach

User.php

  public function sellerOrders()
  {
  return $this->hasMany(OrderProduct::class, 'seller_id');
  }

OrderProduct.php

   public function productInfo()
 {
     return $this->belongsTo('App\Product');
 }

 public function orderInfo()
 {
    return $this->belongsTo(Order::class,  'order_id');
 }

Order.php

   public function user()
  {
    return $this->belongsTo('App\User', 'seller_id');
  }

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

Controller

   $orders = Auth::user()->sellerOrders()->with('productInfo','orderInfo')->get();

When I dd($orders) it shows this

  Collection {#306 ▼
  #items: array:2 [▼
  0 => OrderProduct {#300 ▶}
  1 => OrderProduct {#301 ▶}
  ]
  }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire