Laravel雄辩的一对多关系自定义数据透视表


Laravel eloquent one to many relationship in custom pivot tables

我在mySQL数据库中有一些表,我使用laravel来管理对这些表的访问:

-places
   id
   name
-events
   id
   name
-event_place
   id
   event_id
   place_id
-event_dates
   id
   start_date
   end_date
   event_place_id

然后我在eloquent中为上面的表创建了以下模型:

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
有人能帮我吗?!

每次进行更改时,都必须退出并重新进入Tinker