3.1 Using Tinker
There are many times when we need to test code or learn how to use something, but we don't want to go through the trouble of creating controllers and other PHP pages that end up cluttering our project. Tools like Tinker give us the ability to test and learn without mucking up a project!
1.Introduction1 lesson, 01:29
2.The Basics3 lessons, 40:56
3.Using Eloquent5 lessons, 53:38
4.Relationships2 lessons, 26:36
5.Conclusion1 lesson, 00:59
3.1 Using Tinker
We have our database, we have our data, and we have our model classes. So we have everything that we need to start writing our database-centric code within our applications. But we aren't going to do that, at least within our application itself, at least in this lesson. Because I want to show you a tool called Tinker. This is what's called a read eval print loop, or REPL. Basically it's an interactive shell that we can use to interact with our project. So, the classes that we have created are completely accessible. And we can use them to play around with eloquent and building queries. This is a wonderful tool to use, because if you ever want to just play around with some code or some ideas, or you want to just figure out how something works. You don't have to create a file, edit that file, and then start up an HTTP server, and request that file, no, you just do everything in the console. So it eliminates quite a few of the extra steps that you have to go through as well as the files that are left over from just playing around. So we have to install Tinker. It used to be part of Laravel itself but it was separated into a separate package. So we have to use composer to require Laravel and Tinker. And it could take a few moments to load, but once it does, you have a completely interactive shell for running normal PHP code, but also code that is inside of our project. So the first thing we're going to do once we have Tinker available to us is to run it. And we do so, saying PHP artisan tinker. And then we are now in a PHP shell, so that we can do something like this, we can say a = 1 and we see the evaluation of that statement is 1. We can also say b = 2, and then we can say c = and then a + b, and we have the value of 3. And so anything we do within this particular session of Tinker is going to stay in memory, so that if we refer to any of these variables we're going to see what their values are. But if we stop Tinker, and then whenever we go back into it, then we've lost everything that we did. So if I try to access the A variable we can see that it is undefined as well as C and B. So, let's clear the screen first of all, and then let's start up Tinker. And we're going to use our guitar model class. So, we're going to say appguitar and we want to retrieve all of our guitars. Well, our model class, inherits the model class. If we look at this, which we briefly did, at least as far as manufacturer was concerned. If we look at guitar, we see that it extends model. So, we have all of the methods available to us and one of those methods is all, and this is going to retrieve all of the records within our guitar's database. So whenever we press enter, we see the results, and there are 25 here. But that is what we expect, because we started with 25. So we can also use our model class to find an individual guitar. So, if we say the guitar with an ID of 2, we would use the find method. The find method is for finding a record by the primary key. So we are using the ID here, and whenever we run this, you can see that we get the guitar with the name of Starla and the year of 2015. And we can also pass multiple primary keys to the fine method. We do so with an array, so if we wanted to select the guitar with an ID of 1, 2 and then 22. We can pass in those values and then we get just those guitars. But let's say that we wanted the guitars that were made after 2005. Well to do that we have a layer method. We are going to start building a query here. So we're going to retrieve all of the guitars where the year is greater than 2005, did I say? Well, if I didn't, that's what we're going to get now. Now whenever I press the Enter key, we aren't going to see the results. Instead we see that this evaluates to an object called Builder, this is a query builder. And until we explicitly say that, okay, go ahead and retrieve the records for this query, we are going to have a query builder. So we've specified the query that we want, now we just need to execute that, and we do so with the get method. So, we are using this where static method, the year is greater than 2005, and then we say get. And this is going to retrieve, I don't know how many guitars, quite a few, actually. But then we can also say that we want to order these by year, descending. Which, it kinda looks like it is, although there are some that are out of order. So, we could do essentially the same thing, that we would just chain our method calls. So we're going to say retrieve all of the guitars where the year is greater than 2005. Then we can say orderBy, and we want to order by the year, and we're going to do so descending. And once again, if we press Enter, we get our query builder. So, we have to call that get method in order to actually execute that query. But whenever we do, we see that we have our same list, but it is now ordered by the descending year. So we start at 2017 and we end at 2007. Now let's keep playing with this a little bit. Instead of getting all of these, let's say that we want to get just the first five. So once again, we say where then we have our orderBy, but then we're going to say that we want to take the first five guitars, and so we call the take method. We pass in the value of 5, and then we have to call the get method in order to retrieve those results. And once again, we have our guitars, but now it's limited to 5, one, two, three, four and five. But we can also, instead of retrieving the first five, we could say that okay, we just want the first one. In which case, we don't say take and we don't say get, instead we call a method called first. So our query looks like this, we are retrieving all of our guitars where the year is greater than 2005, we are ordering by the year descending, and then we are going to retrieve the first guitar from those results. And that is our Vela, so the query builder in eloquent is an extremely powerful feature, it allows us to well, build a query. And there are many, many methods for, well, building queries. There are so many that it's hard to cover all of them within even a single video. But there are methods that you will use all of the time like the where method, and then there are others that you will use very rarely. But check out the description for this lesson, I have put links to the documentation. That is really the best place to go to find what methods are available, so that you can build the queries that you need for your application.