我怎样才能定义这种三种雄辩的关系?

时间:2015-05-20 09:53:04

标签: php laravel laravel-5

我有以下设置。事件具有多个运行日期(实例),并且可以在同一日期的多个场所运行。目标是访问活动页面(example.com/event/1)并列出该活动的所有日期和场地。所以我有以下表格:

事件:id,name

实例:id,date,event_id

场地:id,名称

然而,我无法找出将三者联系起来的正确方法。我尝试了belongsToMany(使用instance_venue数据透视表)和hasManyThrough的各种组合,但无济于事:我只是接到一个错误。

最好的方法是什么?

以下是我的模特:

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;
    }
}

这会引发以下错误:

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))

1 个答案:

答案 0 :(得分:1)

知道了:

事件hasMany个实例

实例belongsTo活动和belongsToMany场地

场地belongsToMany个实例