Running a Basic Query0:00
I want to dig into at least a little bit of the Query Builder in this episode. So let's come down and right here let's just execute some query so that we can stop along the ways and figure out what exactly is happening. So why don't we say $posts equals Post::where(id, '>', 160)->get(); and fetch the results. All right, so if I return these $posts and I start up a server, let's take a look. And yeah, sure enough, we get a collection of all Posts that have an id of 160 or greater. So now I want to dig down to the deepest layer, the Query Builder that actually builds up the SQL query string and then passes that over to PDO. All right, so we will do this one step at a time.
Tracing the where Call0:45
the SQL query string and then passes that over to PDO. All right, so we will do this one step at a time. You already learned in the previous episode sort of what happens when we call the where method. Or we went as far as we could, but we didn't really talk about the Query Builder aspect. So let's see. Within the model method, we discussed how there is not a where method on here. We catch it with a magic method and then ultimately defer to Eloquent's Builder class. Now here you'll find a where method where, in this case, we defer once again to a where method on the Query Builder class.
Now here you'll find a where method where, in this case, we defer once again to a where method on the QueryBuilder class. So if we take a look at that one and we look at where, now we can actually dig in. Okay, so let's think about our specific scenario. We said Post where an id, so column is equal to id, operator is equal to greater than or equal to, and then value was equal to 160. So let's figure out what happens here. Well, we didn't pass an array to column, so we can ignore that. We didn't omit the operator, so I can ignore that. And we did pass a valid operator, so we can skip over that.
We didn't omit the operator, so I can ignore that. And we did pass a valid operator, so we can skip over that. Next, id is not a closure, so we can skip that. Further, we know we passed a valid operator, so we can continue on. value is not a closure, so we can skip that. Notice all of these are just quick checks to normalize the values that we're working with. But now, once we've done all of that and we've simplified the query, we come to something just like this. And mostly, you can see that two specific things are happening here.
Storing Wheres and Bindings2:24
just like this. And mostly, you can see that two specific things are happening here. We are building up our where clauses, which is just an array that contains some information that we'll look at. And then we also attach our bindings, because remember, PDO is going to use prepared statements, and this protects us against SQL injection. So we build up the query, and then we attach the bindings when we execute it. So let me show you what this looks like. I'm going to dd the where property and view this in Chrome. If I give it a refresh, now we have an array of arrays, and in this case, we just have
I'm going to die and dump the where's property and view this in Chrome. If I give it a refresh, now we have an array of arrays, and in this case, we just have one item, because we only had one where clause, where id is greater than or equal to 160, so we store it. We have the column name, the operator, and then the value. So that's everything that Laravel will need down the line to actually build up the SQL query. Next, we switch back, we add the binding as well, and that will be, once again, an array of values that we will bind to the prepared statement. So if we take a look at that one, we're just making sure that the type of binding exists.
of values that we will bind to the prepared statement. So if we take a look at that one, we're just making sure that the type of binding exists. In this case, we have a where binding, so we're good there. Next, the value will be, well, if I switch back, in this case, it's 160. Let me show you. dd the value, come back and give that a refresh, and there you go. Okay? So we're just adding that to an array of bindings. So if we scroll down, there you go. This $bindings type equals $value.
So if we scroll down, there you go. This bindings type equals value. So in translation, what we're saying is the bindings array that we will use to build up the SQL query, we're going to add a specific where binding. And we separate these because, remember, there's a few different bindings that you could potentially have for an SQL query. You could use where, or having, or order, or a general select. So all of those need to be tracked. So in this case, we're adding a new item to our where bindings and making that equal to, in this case, 160.
So in this case, we're adding a new item to our where bindings and making that equal to, in this case, 160. Okay, so let's scroll back. If I go back to the where method and we scroll down, just to redo this together, we update our array of wheres. And like we talked about, that contains all the important information that we need there. And then we also update our bindings. And that's all we do there. So if I come back to WelcomeController, we've updated our bindings. Now when we call get, well, that's the method where we will start taking all of those bindings
Executing via get Method4:50
So if I come back to WelcomeController, we've updated our bindings. Now when we call get, well, that's the method where we will start taking all of those bindings and everything we saved, and we will translate that into a SQL query and then pass that over to PDO. Let me show you. If we go back to our Builder class and we call get, okay, we're going to create a fresh select statement. And notice right here, well, when you call the get method, you can pass your columns. You probably knew this. So if we don't pass any arguments, it's going to assume that we're doing a select star.
You probably knew this. So if we don't pass any arguments, it's going to assume that we're doing a select * . But if you don't want that, and you want to be explicit, which is really good in many cases, in most cases, actually, well, then you could do that. And now we're saying we're only interested in selecting title and body. Let's bring that back. And you'll see that it does default to *. So everything. Okay. So we prepare a fresh select statement.
Okay. So we prepare a fresh select statement. We update our list of columns. And then we ultimately process the select statement. But this is the main thing we are interested in. This is where we run the query. If we take a look, you'll see two key methods here, toSql and getBindings. Let me show you what this looks like. This toSql. And as you can imagine, when we run this, that's going to build up your SQL query.
This toSql. And as you can imagine, when we run this, that's going to build up your SQL query. And notice how, just like we talked about, we are using a prepared statement here to protect all of the users who reference these methods against things like SQL injection. So if we will ultimately bind data to this query, well, we need to find the bindings. And we already talked about this. We saved them earlier, which means if we call getBindings, that's just going to return to us that bindings property. All right. So we have the SQL query.
Passing SQL to PDO6:41
All right. So we have the SQL query. We have the bindings. It looks like we have everything we need, which means we can pass this over to our connection. So if we take a look at that, this is actually referencing our database connection. So if we dig in there and we look at the select method that we called, well, we're passing through the SQL query. We're passing through the bindings. So all we have to do is take those and pass them to PDO, like we just talked about. And if I scroll down, you'll see it right here.
So all we have to do is take those and pass them to PDO, like we just talked about. And if I scroll down, you'll see it right here. So we create a new variable $statement, and we get PDO. So if we take a look at all of this. So notice we're just getting an instance of the PDO class, just like you would do if you were writing vanilla basic php. If I scroll to the very top in our constructor, you'll see that gets passed in as a dependency. So now we're at a pretty low level at this point. It should almost look similar to the documentation for how you might use the PDO API. So PDO, prepare this new query.
It should almost look similar to the documentation for how you might use the PDO API. So PDO, prepare this new query. And then when we're using PDO to actually execute the query, you call an execute method. And then as an argument, you can pass through all of the bindings that will be attached to the SQL query that we built up. And then finally, once again, this is just basic PDO stuff. We fetch all of the results using the default mode, which is to fetch an associative array. All right. So I know we went through a lot of hoops there, but at this point, we have triggered and executed that SQL query.
So I know we went through a lot of hoops there, but at this point, we have triggered and executed that SQL query. And granted, in this particular case, we just had a very, very basic query. But now hopefully you can at least begin to see the basic path for how Eloquent takes these method calls and translates that into an SQL query that we pass to PDO.
