簡體   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