I'm getting a call to undefined method error when trying use a custom pivot with a polymorphic many to many relationship.
I setup a morph to many relationship on my page model like so:
class Page extends Model
{
public function media()
{
return $this->morphToMany(Media::class, 'mediable')->using(MediaPivot::class);
}
}
Now when I try and retrieve media from the page, i.e:
$media = $page->media()->get();
I get the following error:
Call to undefined method Illuminate\Database\Query\Builder::setMorphType()
your MediaPivot
should extend MorphPivot
instead of Pivot
My mistake, thank you @roennow
Is this in the docs anywhere? @roennow
It should be documented, I had the same problem.
@roennow is this functionality available at all in 5.1?
@dtturcotte The class has existed since 4.1 so I would guess so. I haven't worked with a 5.1 in a long time but try it :)
Laravel Documentation, which is otherwise excellent, is missing such nuances, forcing us to probe SO and Github Issues for pointers. This is especially true in the case of advanced polymorphic relation concepts. These should ideally part of the documentation, as right now, someone who knows the internals of Laravel can only act as the knowledge base for the rest of us.
@thinkdj you can always help by sending prs to the docs.
Most helpful comment
your
MediaPivot
should extendMorphPivot
instead ofPivot