3.1 Define the Model and Controller
Now we can take what we've learned to start a real project. In this lesson, we'll use Eloquent to create a simple model that connects to a MySQL database. Then we'll write the controller that works with the model.
1.Introduction2 lessons, 06:28
2.Create a Sample API2 lessons, 17:23
3.Write the Music Store API3 lessons, 27:11
4.Conclusion1 lesson, 01:04
3.1 Define the Model and Controller
We are finally ready to write an application, and we're going to start by creating a new project. So in your console, do lumen new and this is going to be a music store. So I'm just going to call this music store. And while this is creating our new project, I'm going to create the database as well. So I'm using phpMyAdmin for that purpose. You don't have to use this if you don't want to, if you have some other tool. And I'm going to call this database music store. Now the reason why I'm creating this now, is because whenever we finally have our project, and we go in and modify the .env file, we need to specify the database information. So it helps if you have a database to work with. So let's go back to the console and our application is ready to go. So I'm going to go ahead and cd in there as well as execute the HTTP server so that we have that running. But I also have another command window that we are going to create our model with. So let me go ahead and cd into there. But before we do anything as far as creating our model, we need to do a few things within the project itself. So let me open up that folder and we're going to start with a .env file. So if you remember we don't have one by default but we do have the example. So I'm just going to copy and paste that. Let's rename it to just .env and we're going to modify the database connection information. Now of course you want to use what values you have, but in my case the database name is going to be music_store, the username is root and the password is root. Yes, it's not very secure but this really doesn't matter on this machine. Everything else as far as my set up is concerned is correct. So mysql is the DB_CONNECTION, the host is localhost, and the port is 3306. So now that we have that done, we need to go to bootstrap and then app PHP because there are two lines that we need to uncomment and here they are. The first is a call to withFacades. The second is a call to withEloquent. So we want to uncomment those lines and save them and now we are ready to create our model. So inside of my other console window we're going to say php artisan and we are going to create a database table called guitars. In fact this is the only table that we are going to create as far as our music store is concerned. I know that there are other instruments but I like guitars so we have guitars. So we want to make a migration and we're going to call that migration create_guitar_table although that should be plural. And then we need the actual create="guitars". So that will create our migration. Now this hasn't actually created the table yet, we need to go into our project, there is this database folder and inside of there is migrations. And now we have a file that is for our creating the guitar's table. And we just need to fill out that table. So we have an ID and guitars are a lot like cars in that we have a make and a model. So let's have a string of make, we'll have another string for model, there's also years involved and serial numbers and so on. But let's just keep the simple, we'll have an ID, a make and model and that's going to be fine. And now we just need to perform that migration. So we'll go back to the command line php artisan and migrate. And this is going to create a guitars table and populate it with those columns. We can see that the table was created successfully. And so if we take a look inside of our admin utility, we should see that there is now a guitars table, and if we look at the structure, then we will see what those columns are. We have the ID, the make, the model created at and then updated at. The next thing we need to do is create our model and we're going to do so inside of the app folder. So let's create a new file just simply called Guitar.php. And we want to inherit eloquence model. Now if we look at this User.php this is actually going to give us quite a bit of stuff that we need. So we can copy that, paste it into the file. Now we don't need these first two use statements, we don't need the last two use statements, but we do need to use Illuminate\Database\Eloquent\Model;. Let's also change the class to guitar. It will extend the model but we don't need to implement anything and we are going to modify the fillable array here. Now you can see that these are the attributes that are mass assignable. It's going to make things a little bit easier for us and that is going to be make and model. We don't need this hidden but we do need to specify the table. So we will say protected $table = and that is guitars. Now because our guitar class inherits from eloquence model, we have all of the basic crud operations out of the box. So from here we just need to create our routes. But first let's create the controller because there are a few things that we need to talk about before we actually create our routes. So let's go to http controllers. Let's start with the example controller and let's rename that to guitar controller. You'll also need to change the class name as well. And we also need to use a few things. The first is our guitar model. So we want to use App\Guitar. We also want to use Illuminate\Http\Request because it we are going to use the request in order to retrieve all the information to easily create an update, a guitar. And we will talk about that here in a few moments. So the first method that we need here is something that is going to return all of our guitars. So that's the index essentially. So we are going to retrieve all of our guitars by saying guitar and then all and then we want to return those guitars as a JSON structure. So we're going to say response and then JSON. And we will pass in our guitars, and that will give us our JSON structure. So we have the index, the next thing we need is an individual guitar. So let's just call this method find, and this is going to be the ID of a guitar and then we will say $guitar = Guitar and we want to find the guitar with the given ID. And then we will do the same thing as the index method. We will call response. Then we will call JSON and pass in Guitar. Now let's write a method for creating a guitar. So we will call this create. And in this case we are going to say request and then request. Because this is what's going to happen. Lumen is going to see that we have a type hint for our request. And it's going to supply that object to us so that we can use it within this create method. And that's how the dependency injection works. You use a type hint and it gives you what you need. So in this case we're using the request so that we can easily create a guitar. So let's say guitar = and we want to create, and this is where we will use our request. We're going to pass in the result of calling the all method, and then we are going to return that guitar. So let's just take code from the previous method and paste that in. Now I'm going to ignore this red squiggly because I'm not really sure how well Visual Studio code syntax rules are for PHP. Whenever we actually run this, we will find out. So let's have a method for update. And this is going to be somewhat similar to the create in that we are going to have our request, but then we also need data from the URL and that is going to be the ID. So when you need data to be injected into your method, you start with the things that you need, and then after that you have your parameters from the route. So this is going to be where we find a guitar with a given ID, we want to update that guitar. So we are going to call the update method. Once again calling the all method for the request and we're going to do something similar to all of the other methods. We will still return JSON, but instead of returning just the guitar, we're going to say that it is updated and the value is going to be our guitar. And then finally we need a delete method. So public function, we'll call this deletes. We want the ID of the guitar to delete. We will create a variable called count, where we will destroy the guitar with the given ID and then we need to know if that guitar was deleted. So we are still going to return JSON, but in this case it's going to be deleted and then the value here is going to be whether or not count is equal to one. So now we have a database. We have a model. We also have a controller. But we don't have any routes so we will write that as well as run our application in the next lesson.