framework
framework copied to clipboard
newPivot function is not called when using Polymorphic Relations
Hi, I'm using polymorphic relationships in my project. I wanted to create my own model representing Pivot. However, when referring to the polymorphic relationship, newPivot function is not being caled. Sample code:
public function notifications()
{
return $this->morphToMany('Notification', 'notifiable')
->withPivot('notice_time', 'user_seen')->withTimestamps();
}
public function newPivot(Model $parent, array $attributes, $table, $exists)
{
//This function is not being called.
return new Notifiable($parent, $attributes, $table, $exists);
}
Is there any way that referring to the object->pivot->function() will work?
I had the same problem, but may have found a solution. The problem is that polymorphic relationships don't use a standard Pivot model (created via the newPivot function), but instead they use a MorphPivot model. So I added the "newMorphPivot" function to Eloquent\Model and changed one line in Relations\MorphToMany to make it work. Preparing a pull request as we speak.