01. A look at the query method


In this course, we're going to take a look at Eloquent's query method in Laravel and a few different ways that you can use these in your Laravel apps. So first of all, let's just take a look at what we would do as building up a normal query.
So what I've gone ahead and done here is create a fresh Laravel app. I have gone ahead and created a post model with, of course, a table, and I've just got two posts in here. And this is pretty much all the data that we're going to need to work with this.
So let's go ahead and look just really quickly at how we'd grab a post out perhaps by its slug. So let's go ahead and create out a variable called post. Let's go ahead and use post here and we would just start to chain on say the where method and we go ahead and pass in the slug here and we would say post one
and then we'd use a method like first or fail something like that. So let's just go ahead and pull the namespace in for post and we can go ahead and die dump out on that post. That should give us back the post from our database, of course, which matches that slug.
Now what we can also do here is go ahead and just use the query method and then start to chain on with where. If we head over to the browser and just refresh this you can see that this works in exactly the same way. So really just adding this in seems a little bit pointless at this point.
Before we go ahead and look at where query can be useful though, let's just examine what's actually happening when we're using query. In fact before we do that, let's look at what happens when we statically access this where method. So if we open up the post model here, we know that this extends the base model.
So if we open up the base model from database and eloquent and we look for say a where method, this doesn't actually exist in here. What does exist though is a call magic method and what this does just down here unless there are any other conditions that are met,
it goes ahead and forwards this call or forwards this method and any parameters we give it to the result of this new query. So let's go ahead and look for new query and you can see here that this will go ahead and call this register global scopes method
and new query without scopes. So let's again go ahead and look at this new query without scopes and you can see that this calls new model query. Let's go further into this and open this up
and you can see this goes ahead and gives us a new eloquent builder. Once again, let's go to new eloquent builder and we're finally at the point where you can see this is returning to us a builder class. So let's open up that builder class.
So make sure we do this under database and eloquent and let's have a look for the where method and sure enough there exists with all of the parameters that we would usually expect to pass through. So what we're actually doing if we head straight back to our roots here when we call
where is actually retrieving out that builder instance that we've just seen and then go ahead and start to chain on where and then build up our queries that way. So that's what happens when you pretty much access any of these static type methods on a model.
Now if we just go ahead and pull back our query here, we know that that works in exactly the same way but what we can do is we can take this and just die and dump on it up here just to prove that that is exactly what we get back
and you see we've got an eloquent builder back here and that's why we can continue to chain where on. So that's pretty much what is happening behind the scenes. But the question is why would it ever be useful to actually use the query method
instead of just accessing where or any of the other methods that you would expect from eloquent? Well, that's what we're going to look at in the course. So let's go over to the next episode where we're going to look at some visual appeal of using query.
7 episodes 23 mins


Ever used the Eloquent query() method in Laravel before? In this short course, we'll dive into what this method does, and demonstrate the ways it can be used.

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


No comments, yet. Be the first to leave a comment.