我在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('AppModelsEvent', '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('AppModelsPlace', '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('AppModelsEventDate', 'event_place_id');
}
}
class EventDate extends Model {
public function eventPlace() {
return $this->belongsTo('AppModelsEventPlace', 'event_place_id', 'id');
}
}
当我运行这段代码时:
$dates = AppModelsEventDate::find(1)->eventPlace->toJson();
dump($dates);
此错误出现:
Argument 1 passed to IlluminateDatabaseEloquentRelationsPivot::__construct() must be an instance of IlluminateDatabaseEloquentModel, none given, called in /Users/hamidzamani/Sites/mycity/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 827 and defined
有人能帮我吗?! 每次进行更改时,都必须退出并重新进入Tinker