Ricardo Plompen
Ricardo Plompen

Reputation: 115

list of products in an foreach with an foreach

For school I got a project to make an webshop so I try to make an admin dashboard where if u wanna see the users informatie, u also will see the orders the user placed.

Right now i try this

@foreach($user->order as $order)
      <div class='col-sm-12 col-md-6 col-lg-6'>
        <div class="card">
          <div class="card-header">
          <b>Order ID:</b> {{ $order->id }}
          </div>
          <div class="card-body">
            <ul class="list-group list-group-flush">
              <li class="list-group-item list-group-item-secondary text-center text-danger"><b>Info:</b></li>
              <li class="list-group-item"><b>Datum:</b> {{ $order->date }}</li>
              <li class="list-group-item"><b>Prijs:</b> € 200</li>
              <li class="list-group-item list-group-item-secondary text-center text-danger"><b>Producten:</b></li>
              @foreach($user->order->orderrow as $orderrow)
                <li class="list-group-item">{{ $orderrow->product->productname }}</li>
              @endforeach
            </ul>
          </div>
        </div>
      </div>
    @endforeach

but how can i make it that when i want to see the orderrow from an other table then the order table. is able to be worked in an foreach on the users.show.blade.php

Product model

public function orderrows()
{
    return $this->hasMany(Orderrow::class)->orderBy('date', 'desc');
}

Orderrow model

public function product()
{
    return $this->belongsTo(Product::class);
}

Order model

public function user()
{
    return $this->belongsTo(User::class);
}

public function orderrow()
{
    return $this->hasMany(Orderrow::class);
}

User model

public function order()
{
    return $this->hasMany(Order::class);
}

Upvotes: 1

Views: 350

Answers (1)

Akshay Kulkarni
Akshay Kulkarni

Reputation: 740

Try using this code for inner foreach loop.

@foreach($order->orderrow as $orderrow)
    <li class="list-group-item">{{ $orderrow->product->productname }}</li>
@endforeach

Upvotes: 1

Related Questions