Using WhereHas in Laravel Polymorphic Relations
Techniques like these are important to document because we bump into them frequently, but not frequently enough to have it memorized!
It seems like once every project I find myself googling "Laravel Polymorphic WhereHas", combing through one result after another, frustratedly repeating "How do I do this???".
While there isn't a first-party supported solution, I wanted to document what has been working for me, so I don't have to continue searching StackOverflow!
Sorting Parent Eloquent Models by a Child Relationship
Recently I had a situation in a Laravel app where I needed to sort a collection of parent models by a particular value in aBelongsTo()
relationship field.
Itβs fairly simple to sort the opposite direction, meaning, to sort all the related models that belong to a parent model.
Replace Conditional with Polymorphism
Polymorphism is an extremely helpful technique to master. Take a look at this article to see it implemented in a Laravel application:
If you've ever done any research into refactoring, or programming in general, you've most likely heard the term "polymorphism". When I first came across it, I have to admit, I was intimidated.
Now that I've become more familiar with the concept, I can assure you, the word itself is more complicated than the underlying principle!
Page 2 of 2 (14 total entries)