Ok, finally got this 'working'.
Models:
class Event extends Model {
public function instances()
{
return $this->hasMany('App\Instance');
}
}
class Instance extends Model {
public function events()
{
return $this->belongsTo('App\Event');
}
public function venues()
{
return $this->belongsToMany('App\Venue');
}
}
class Venue extends Model {
public function instances()
{
return $this->belongsToMany('App\Instance');
}
}
And the controller:
class EventsController extends Controller {
public function show($id)
{
$event = Event::with(['instances.venues'])->where('id','=',$id)->get();
return view('events.show',compact('event'));
}
}
And the view:
@foreach($event->instances as $instance)
<span>{{ $instance->date }}</span><br/>
@foreach($instance->venues as $venue)
<span>{{ $venue->name }}</span>
@endforeach
@endforeach
This throws:
ErrorException in 0b743a0698b88f6d9e2df13521b60750 line 1:
Undefined property: Illuminate\Database\Eloquent\Collection::$instances (View: /home/vagrant/Code/PivotTest/resources/views/events/show.blade.php)
If I just return the variable, I get the following JSON:
[
{
id: 1,
name: "First Event",
description: "This is the first event",
created_at: "-0001-11-30 00:00:00",
updated_at: "-0001-11-30 00:00:00",
instances: [
{
id: 1,
date: "2015-06-01",
event_id: 1,
created_at: "-0001-11-30 00:00:00",
updated_at: "-0001-11-30 00:00:00",
venues: [
{
id: 1,
name: "New York",
address: "1 5th Avenue",
created_at: "-0001-11-30 00:00:00",
updated_at: "-0001-11-30 00:00:00",
pivot: {
instance_id: 1,
venue_id: 1
}
}
]
},
{
id: 2,
date: "2015-07-21",
event_id: 1,
created_at: "-0001-11-30 00:00:00",
updated_at: "-0001-11-30 00:00:00",
venues: [
{
id: 2,
name: "Los Angeles",
address: "3 Sunset Boulevard",
created_at: "-0001-11-30 00:00:00",
updated_at: "-0001-11-30 00:00:00",
pivot: {
instance_id: 2,
venue_id: 2
}
}
]
},
{
id: 3,
date: "2015-11-13",
event_id: 1,
created_at: "-0001-11-30 00:00:00",
updated_at: "-0001-11-30 00:00:00",
venues: [
{
id: 1,
name: "New York",
address: "1 5th Avenue",
created_at: "-0001-11-30 00:00:00",
updated_at: "-0001-11-30 00:00:00",
pivot: {
instance_id: 3,
venue_id: 1
}
}
]
}
]
}