FREELessons: 12Length: 2.1 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.3 Creating Models and Factories

We interact with our databases using what are called "model classes". They're programmatic representations of our database tables, and I'll show you how to create them in this lesson. We'll also revisit our seeding code to use our models and factories.

2.3 Creating Models and Factories

So we have created our database, we defined the schema, and we also seeded our database with data. Now, we just want to be able to access our data, and we can do so in a couple of different ways. The first and the most basic way is to simply use SQL. So if we go to the, not the migration, so if we go to the seeds and we look at the class, if you remember we use this DB class, and we could use this DB class in order to issue SQL statements, and I'm not going to say that that's wrong but that kind of goes against the idea of using an ORM to begin with, because one of the ideas of an ORM is to hide SQL. So instead what we typically do is create what are called model classes. These are classes that pretty much directly map to our individual tables. And then we would use those classes to access the data inside of those tables. So we have two tables that we are really concerned with, guitars and manufacturers. So we would create two model classes for these tables. And the convention is to call our classes the singular noun of our table. So we have guitars, which makes a sense because we store multiple guitars inside of our guitar's table. But as far as our model class is concerned, we would just say guitar, and it would be with a capital G. So we create a model class using Artisan, so artisan make model and then simply Guitar, and this is going to create a model class called Guitar. And let's go ahead and do manufacturer as well, so manufacturer that will give us that model class. So if we go and look at app and then directly inside of app, we have our guitar class and we have our manufacturer class. Now, eloquent is going to make some assumptions based upon the name of our classes. It will see that this class is called manufacturer. So it is going to look for a table called manufacturers in our database, and in this particular case, that's okay. That is how we set this up. But in some cases, you might not have the luxury of having your model name be the same name or something similar to the table name in the database. So in those cases, what you could do is set a property called table. It is a protected property. And you simply say table, and then, you specify the name of that table. So if your table name were my_manufacturers, he would say, my manufacturers and then eloquent would look for this table in the database and it would use that. The second assumption is the name of the primary key which is RID field by default it's going to look for IED but once again if you are not fortunate enough to have a table where the primary key is called id, it would be called manufacturer id in this case, then there is another protected variable called primary key, and would set that to whatever value that you had. So in this case, it could be manufacturer id and so on and so forth. Now, of course, in our case, this is not what we want to do because we follow the convention. Our table name is the plural of our model name, and the primary key is ID. However, I'm going to comment this out, just so that it is there in the code download, so that if you ever need to refer to it, then there it is. But, of course, this is also in the documentation as well, so you can find that very easily at Now, we will eventually get around to using these classes to retrieve the data from the database. But before we do that, I want to revisit the topic of receding because now that we have some model classes, we can take advantage of some useful utilities to make seeding our database a little bit more intuitive. So the first thing that we're going to look at is something called a factory. If you go to the database folder and then there's this factories, and there is a user factory Inside of this file. You can see that there is this factory object that has a method called define and it is creating a factory for the user class, and then there is this function that's going to execute that is the factory itself, so that we can use this factory. In order to create a new user, that's essentially what this is doing here. And you can see that it's using something called a Faker. So it's creating a fake name. It's creating a fake email address. And so instead of just generating random strings, we could use this faker so that we have something a little bit more meaningful to work with within our data. So we can create a new file inside of this factory's folder. Any file that we create is going to be automatically loaded, so we don't have to go to anywhere else within our project and specify that, hey, there's this other factory that we want you to use. And we're going to create a new file called guitarfactory. And we're going to follow the same pattern as what's being done here. So let's just copy what we have inside of the user factory. And let's paste that into guitar factory. And we want to change the first argument to define, instead of user this is guitar. And we are still going to use this faker object, but now we just need to build the guitar itself, and really we just have the name, so we can get rid of the email, the password, and the remember token, and let's also get rid of this static password as well. So this is going to create a new guitar, and it's going to give us a name that is not just random characters. So that's, we can now go our seeder. Let's go to guitars seeder, let's get rid of this for loop, and we're going to use that factory. So we will say factory, and then we will pass in the model class that we want to use. That was App and then Guitar. And then we can specify the amount of Guitars that we want to create. We used 20, so we could do that. And then we will use the create method and we can also modify this code as well. Since we have a model class, we don't have to use the DB class anymore. We can simply just say app, and then guitar, and then we can call the truncate method. But in this case, it's not going to be an instance method it is a static method. So we will say app \Guitar, and we will call truncate. And we can also get rid of this DB usage as well. We have our model class. It has a method called create. It's still accepts the same data, so we don't need to change that, but we have simplified our code, and it's a little bit easier to read as well. So let's go ahead and make the necessary changes on these other lines. So we will just change out the old DB code to use our model class. And now, we'll just need to test this out, although this is not a variable, that is a function. So let's go to the command line, php. Artisan DB seed that is going to run our seeds, and let's look at the results. So if we go to the guitars table, we have our named guitar Guitars or our hard coated guitars, I should say. And then [LAUGH] we have some very uniquely named guitars. Valentine, I'm not gonna try to even pronounce that. But there we go, we have some names that are a little bit better than just some random characters. But now let's do this. We really need a year within our guitars table. And we could modify our existing migration for whenever we created the guitars table, we could add in a column there. But let's create a separate migration. One that is going to add that column in. So we will say php artisan make migration and we're gonna call this add_year_to_guitars_table. That will make this migration and we of course want to open this up to write our code. Let's go to the migrations folder and inside the up we are going to add this code. We're going to say Schema Table, and we want to specify our guitars table, and this is going to accept a function. And the function is going to have a blueprint object, so we'll call this table. We're going to use this table object in order to add that column. So we will say table, and we want An integer, really we want something smaller than an integer but an integer is going to be fine, and we will call it year. So now inside of our guitars table seeder we need to add a year here, so let's do that. We'll say year for the velar is going to be 2017. We'll say that the was 2015. And then, the Les Paul I would really like for this to be 1959, but that's not the case. The one I have is 2012, actually, it's a 2011. The Explorer I don't have, but let's make this a little older. Let's say 1970. I really don't know if that's a good year for an Explorer. And then, for a Strat, let's see I don't remember the year of the one that I have so let's make one up. Let's make this rather old, a 1956. Okay, so that's great that we have years here, but we also need years for all of our fake guitars. So if we go to our factory, we're going to use the faker. We need to specify the year here. So we will say faker, and we have a method called year. And we can specify the max year that we want, which is now. This means that we are going to end up with some pretty old guitars probably, but that's okay in my opinion. Once again, this is just Information that we just really don't care about. Okay, so here's the thing. We have a new migration and then we want to reseed our database. So one thing we could do is perform all of our migrations and then seed. This is a great way to essentially refresh our database so that we can start completely over from scratch and that sounds great. So we say php artisan, and we say migrate, and then refresh but we also to seed as well. So we will say dash dash seed, and this is going to roll back all of our migrations, it's going to migrate everything once again, and then it's going to seed our database. So if we go look at the data, we will see our guitar's table. We now have a year over here and let's look at some of the generated years. They're not that bad. So that turned out better than I thought it would, and, of course, we didn't modify the manufacturers at all. It was just the guitars, cedar. But using the refresh command rolls back all of your migrations, and then executes those migrations, and then seeds the database. So now, we have everything that we need to start working with our data within our applications. In the next lesson, we are going to kind of do just that but instead we're going to use a tool called Tinker which will allow us to tinker with our models.

Back to the top