Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent

I've 3 models:

  • User
  • Order
  • Billet

User: DB:

-id
...

Model:

	public function orders() {
		return $this->hasMany('App\Order');
	}

	public function billets() {
		return $this->hasManyThrough(App\Billet', 'App\Order');
	}

Order: DB:

-id
-user_id
...

Model:

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

Billet: DB:

-id
-order_id
-url
...

Model:

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

Controller:

$orders = User::find($id)->orders;
$billets = User::find($id)->billets;
//
return view('my.view', compact('orders', 'billets'));

The problem:

I can't list $orders and $billets togethers using the same foreach on my view...

Separete works:

@foreach ($orders as $order)
{{ $order->name }}
@endforeach

//

@foreach ($billets as $billet)
{{ $billet->url }}
@endforeach

I needed something like:

@foreach ($orders as $order)
{{ $order->name }} {{ $order->billet->url }}
@endforeach

I need $order->name and billet->url together, associated...

Separate work. List all billets related to the user. But I can't associate the same foreach User + Billet + Order.

Any idea?

Thanks.

Last updated 3 years ago.
0

Have you tryed nested loops?

0

Yes. But didn't work because not all orders have a billet. So everything is messy... basically I need to access the billet (and their records) through the order and user (User->order->billet->url).

Thank you.

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.