Playing
01. Refactoring chained where methods in Laravel

Episodes

0%
Your progress
  • Total: 2m
  • Played: 0m
  • Remaining: 2m
Join or sign in to track your progress

Transcript

00:00
When you're using Laravel's Query Builder, you're probably used to doing something like this, chaining where clauses onto each other if you need to check for multiple things. So in this case, just a really simple example which could actually be abstracted away to a scope.
00:16
But nevertheless, this is what you would normally do. So we're grabbing an article by the slug and also checking whether it's live. So sure enough, this works. I just have one article over in the database here.
00:27
But if you are doing anything more complex than this, you'll probably find you have lots of different where clauses. And the kind of convention is to pull these down to the next line. Sometimes this gets a little bit messy and you may wish to tidy it up.
00:41
So we're going to take a look at another way of doing this. So let's just copy this down here and comment this one out. And let's go ahead and just use a single where and see what we can do. Now just a little bit of background behind this, if we open up the Query Builder within
00:56
Laravel and we go down to the where clause and open up this method, you can see here that if the column that we pass in, e.g. the first option that we pass into this where method is an array, then we actually return using this array of where's method. So let's open this up, add array of where's.
01:16
So what this is going to do is it's going to take each of the items inside of an array and it's going to build these up accordingly. And it's going to go ahead and nest them where's. So let's just take a look at how this works first of all.
01:28
Really, really simply what we can do instead of chaining these on is say slug some article and we can say live is true. So that is the easiest way to get this to work. So really simply that will give us exactly the same results.
01:43
Already nicely cleaned up. And this really helps if you're working within a class, you could create a separate method to grab these where's if you needed to. It just helps to really tidy stuff up despite the fact that you could move this over to
01:55
a scope. Now you're probably thinking, well, you know, these are pretty simple values. We're doing equality checks here, much like what is happening here. But actually what's going to happen is, is if we have an array for the value that we're
02:07
passing in and we have a numeric key, e.g. we're doing something like this, which would give this a numeric key of say zero or one, then we can actually do a similar thing but pass a third argument in. So if we just open this up, instead of doing an inequality check, this is grabbing out
02:26
the array values from that array that's inside of here. And that means that we can use a operator inside of here. So we can actually use operators if we pass a full array into here. You can see that that works in exactly the same way.
02:42
So a really simple way to tidy up your code. Of course, like I said, it would be nice to have some kind of scope for this. But if a scope isn't necessary and you need to go ahead and nest where's, next time you might find this single where call a little bit cleaner.
1 episode 2 mins

Overview

If you're chaining where methods when building queries with Eloquent, check out this simple refactoring tip.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!