I have the following setup. An Event has multiple dates (Instances) on which it runs, and can run at multiple Venues on the same date. The goal is to visit the Event page (example.com/event/1) and have it list all the dates and their venues for that event. So I have the following tables:
events: id, name
instances: id, date, event_id
venues: id, name
However, I can't work out the correct way to relate the three together. I have tried various combinations of belongsToMany
(using an instance_venue
pivot table) and hasManyThrough
but to no avail: I just get one error after another.
What's the best way to go about it?
Here are my Models:
Event.php
class Event extends Model {
public function instances()
{
return $this->belongsToMany('App\Instance');
}
}
Instance.php
class Instance extends Model {
public function events()
{
return $this->belongsToMany('App\Event');
}
}
Venue.php
class Venue extends Model {
public function instances()
{
return $this->belongsToMany('App\Instance');
}
}
EcentsController.php
use App\Event;
class EventsController extends Controller {
public function index()
{
return 'Index!';
}
public function show($id)
{
$event = Event::with(['instances.venues'])->where('id','=',$id)->get();
return $event;
}
}
This throws the following error:
QueryException in Connection.php line 620:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'pivottest.event_instance' doesn't exist (SQL: select `instances`.*, `event_instance`.`event_id` as `pivot_event_id`, `event_instance`.`instance_id` as `pivot_instance_id` from `instances` inner join `event_instance` on `instances`.`id` = `event_instance`.`instance_id` where `event_instance`.`event_id` in (1))
Got it:
Event hasMany
instances
Instance belongsTo
Event and belongsToMany
venues
Venue belongsToMany
instances
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.