framework icon indicating copy to clipboard operation
framework copied to clipboard

newPivot function is not called when using Polymorphic Relations

Open daverdalas opened this issue 11 years ago • 1 comments

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?

daverdalas avatar Jul 31 '15 17:07 daverdalas

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.

jvdyck avatar Aug 07 '15 10:08 jvdyck