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!
Programming for your Future Self
If you are anything like me, you may find yourself hacking on a variety of different applications during a given week. Occasionally I get the luxury of working on a single app, which gives my brain the freedom to immerse itself into the details of the program. However, inevitably at some point I have to set the project aside and fire up a different one… perhaps one that was written months, or even years ago.
Several thoughts have repeatedly surfaced as I re-familiarize myself with an old project. Instead of having to collect them each time, I thought I’d write them down here, to serve as a reminder.
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 a
It’s fairly simple to sort the opposite direction, meaning, to sort all the related models that belong to a parent model.
Simplifying Conditional Expressions
As I’ve been reading through Refactoring by Martin Fowler, I’ve found it helpful to rewrite some of the examples from the book in PHP in order to cement the concepts into my mind. While Martin’s examples are primarily in Java, I’ve found an overwhelming majority of the concepts apply to PHP, which is where I spend most of my programming time.
In today’s article, I will attempt to rework the Simplifying Conditional Expressions (pp. 237-270) section into a handful of PHP-based examples.
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!
Refactoring Techniques: Extract Method
A while back, I’d thrown out a question in the Laravel Slack channel asking people what the “must-reads” were for devs. Along with the obvious Uncle Bob books, someone (I believe it was Matt Stauffer) mentioned Refactoring - Improving the Design of Existing Code by Martin Fowler. I looked it up on Amazon and while it was available, I found myself questioning whether a book that was dated 1999 would have any validity in the “modern” coding world I find myself in… not to mention that it didn’t appear to be written for PHP, which is the primary language I use.
I’m glad to say that I put aside my doubts, followed the recommendation, and picked up a copy!
Initially I was disappointed to see that the examples are written in Java, a language I know nothing about. However, I’ve been surprised to see how the broader concepts, such as organization and classes, relate very similarly to what I find myself dealing with in the PHP/Laravel world.
Diving Into a Laravel Audit
Recently the Zaengle team was asked to look over an existing Laravel application and give a review of the overall state of the code. We were to act as an independent third party, between a new studio who had inherited the codebase, and the client, who was anticipating launching the product. The studio had some concerns about the quality of the code and wanted an independent review prior to picking up development on the project. Here are a few thoughts on the process I followed and several takeaways I had from the experience!
Testing 101: Escape Testing Paralysis!
Learning to write good tests is critical to being a programmer. We write tests to make sure the work is solid and doing what it's supposed to, while exerting as little effort as possible (we're not lazy; we're effective...). When I understood that, I determined that I was going to get great at testing. So naturally I started internet research: "Dear Google, show me 'best code testing practices.'"
I quickly gathered that there are a multitude of testing solutions with numerous opinions on each one. In the same way that children can choose blue LEGO bricks, red ones, or two green ones and then argue loudly about which choice is superior, I heard terms like unit testing, functional testing, integration testing, acceptance testing, etc., and all the debates on what should and shouldn’t be tested in our applications.
I soon found myself in testing-paralysis. My overloaded brain said I had to thoroughly understand each of these paradigms before I could begin implementing any kind of testing on my apps. (Which color LEGO is best?) I didn’t feel confident enough to implement any test, for fear of choosing the wrong test, so I resorted to saying, “Let’s just build the app, and I’ll circle back and test it later.”
Little did I know that even though I thought avoiding the tests was making my life easier, I was actually digging myself into a hole.
Page 5 of 5 (48 total entries)