2015-08-16 51 views
0

我在MySQL數據庫中的一些表,我用laravel管理訪問到這些表一對多的關係:Laravel雄辯一個自定義數據透視表

-places 
    id 
    name 

-events 
    id 
    name 

-event_place 
    id 
    event_id 
    place_id 

-event_dates 
    id 
    start_date 
    end_date 
    event_place_id 

然後我創建了以下型號的雄辯的上面的表格:

class Place extends Model { 

    public function events() { 
     return $this->belongsToMany('App\Models\Event', 'event_place', 'place_id', 'event_id')->withPivot('id'); 
    } 

    public function newPivot(Model $parent, array $attributes, $table, $exists) { 
     if ($parent instanceof Event) { 
     return new EventPlace($parent, $attributes, $table, $exists); 
     } 
     return parent::newPivot($parent, $attributes, $table, $exists); 
    } 
} 

class Event extends Model { 

    public function places() { 
     return $this->belongsToMany('App\Models\Place', 'event_place', 'event_id', 'place_id')->withPivot('id'); 
    } 

    public function newPivot(Model $parent, array $attributes, $table, $exists) { 
     if ($parent instanceof Place) { 
     return new EventPlace($parent, $attributes, $table, $exists); 
     } 
     return parent::newPivot($parent, $attributes, $table, $exists); 
    } 
} 

class EventPlace extends Pivot { 

    public function event() { 
     return $this->belongsTo('Event'); 
    } 

    public function place() { 
     return $this->belongsTo('Place'); 
    } 

    public function eventDates() { 
     return $this->hasMany('App\Models\EventDate', 'event_place_id'); 
    } 
} 

class EventDate extends Model { 

    public function eventPlace() { 
     return $this->belongsTo('App\Models\EventPlace', 'event_place_id', 'id'); 
    } 
} 

當我運行這段代碼:

$dates = App\Models\EventDate::find(1)->eventPlace->toJson(); 
dump($dates); 

這種錯誤上升:

Argument 1 passed to Illuminate\Database\Eloquent\Relations\Pivot::__construct() must be an instance of Illuminate\Database\Eloquent\Model, none given, called in /Users/hamidzamani/Sites/mycity/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 827 and defined 

有人能幫幫我嗎?!

+0

每當您進行更改時,您都必須退出並重新輸入修補程序。 – zeratulmdq

+0

我得到同樣的錯誤! 您是否找到答案或解決方法? –

+1

@zeratulmdq評論是正確的答案,請將其轉換爲接受答案 –

回答

0

每當您進行更改時,都必須退出並重新輸入修補程序。

相關問題