繁体   English   中英

如何定义这种三向口才关系?

[英]How can I define this 3 way Eloquent Relationship?

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

事件:ID,名称

实例:id,日期,event_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))

得到它了:

事件hasMany实例

实例belongsTo事件和belongsToMany场所

地点belongsToMany实例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM