Source: tighten.com

Encapsulating Polymorphism
Programmers aiming for simple, concise code often work to remove conditionals. Polymorphic relationships are such a pattern—a powerful tool that can help us avoid complicated code paths when we’re dealing with similar related items.

For images attached to a post, we’ll display this one: {$post->name} was posted on {$post->created_at}.

{ - return $this->posts->append($this->videos->toArray()); + return $this->hasMany(Taggable::class); The problem with this approach is taggables doesn’t actually return the tagged things.

We really want to replicate the pattern introduced in the Imageable model by having the taggables relationship return the things that implement that contract.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners