2.1 Project Setup and Migrations
Welcome! In this lesson we'll create our project and configure its environment so that Eloquent knows where our database is. We'll also create our tables—using migrations instead of typing all that SQL nonsense.
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
2.1 Project Setup and Migrations
We are going to start at the beginning, now not the very beginning because I assume that you have an environment already set up. So you have PHP. You have MySQL and you know how to create Laravel projects. Now if you don't have any of those things or you don't know how to create Laravel projects, then I encourage you to just stop right now, and look at the links in the description for this lesson. Because there are links to tutorials and other courses here at tuts+ to get you up and running as quickly as possible with those topics. So you need a basic understanding of PHP because we are going to be writing PHP. You also need a basic understanding of Laravel itself. Now, we are focusing on Eloquent but Eloquent is part of Laravel, and we are going to be doing some things with Laravel in order to work with the data that we're using Eloquent to work with so please keep that in mind. I don't want you to get lost because I want you to flourish down this path of being a web-developer. So if you're still here, let's go ahead and create a new project. I'm going to call this guitars because I want to work with data related to guitars. You can call this whatever you want. But if you want to follow along call yours guitars as well. And while our project is being created, I thought that I would give you an overview as to what Eloquent is. Now, in the introduction, I mentioned that Eloquent is an ORM. It stands for object relational mapping or mapper. I actually don't remember what the M actually stands for, I know it's map something. But the whole idea behind an ORM is to hide all of the needy-greedy of working with a data base. Because that can be, well, less than ideal. If you're like me and the many other developers, you don't really like using SQL within your applications. So the first thing that an ORM does is it hides all of that stuff. So you don't have to write SQL statements or do anything like that instead you use objects. And that's the second part of what an ORM does. It takes the data coming from the database and converts them into objects that we can then use within our application so that we're using something that actually makes sense within our application. Sometimes, things can get a little wonky if you're using SQL. But if you're just using objects then everything just kind of works together. But it also converts those objects into SQL statements that we would then execute in order to insert, update, or delete things. So it does everything for us as far as working with the database and working with the data contained within our database. But the first thing we have to do is create a database ourself because Eloquent is not going to do that for us. So fire up whatever tool that you want, when it comes to working with a database I like a graphical interface, but that's just me. Some people like to use a command line, and if you're one of those people, then more power to you. But basically, we just need to create a database, and I'm going to call this guitars. Because this database is going to contain information regarding guitars, so it kinda makes sense. So once you have a database then you're really good to go. We aren't going to spend a whole lot of time looking at our database other than to say, hey, this is what we have done without writing any SQL, so it's really nice that way. So let's go ahead and fire up our code editor because the first thing we need to do is modify our environment. We have set up our database but we need to tell our environment where that database is. So that any of the commands that we issue with Artisan will know where to actually create the stuff that we want to create. So inside of the .env file we have this section of DB_, and then there's connection, host, port, database, username, and password. Now, this information is going to probably be different for you because it really depends upon how you're environment is set up. So if you're using homestead, then you're pretty much done. You'll probably need to change the database name but other than that, you're good to go. As far as I'm concerned, I need to change the database to guitars, the username to root and the password to root. But everything else is going to be the same. And once again, this is dependent upon your particular setup but once you have made those changes then we are ready to write our migrations. But before we do that, let's look at the database folder, let's go into the migrations folder, and you can see there are two migrations already here. There's one for creating the users table. Then, there's one for creating the password resets table. Now, take a second and notice what we have here. We have just PHP code. We're not doing any of those weird incantations to create a table using SQL. So you don't have to remember that syntax, you don't have to remember the database, data types. All you need is to know PHP and know that API for creating a table so we are going to create a migration. Now, a migration is really a very, very wonderful tool. It allows us to not only create new things within the database like tables, but it also allows us to modify them. We can also roll back any of our migrations so that's, well, let's look at one of these, the create users table. There is a method called up that is for actually doing what this migration is for. This is for creating the users table. But then, there's a method called down. This is whenever you want to roll back a particular migration, and the code here is to simply drop the users table if it exists. So a migration is wonderful for end users because it allows them to set up their database with just a couple of commands. But it's also great for developers as well because we can create our migrations. We can work with them, and then if we need to rollback or just wipe everything out and start completely over, we can do that by rolling everything back. So it's a very, very wonderful feature, and most ORM's, regardless of language or platform, has this idea of migrations. So we want to create a migration for creating our first table. So let's first of all cd into our project. And then, we will say PHP artisan. And then, we have the make command and we want to make a migration. And then, we give it a name and the name needs to be descriptive as to what we are doing. So we're going to create a table called guitars. So create Guitar_table, and then press enter. And we see that it created the migration that puts the date and the time, I assume that's the time. And this file is going to be added to our migrations folder. So we have the create_users, the create_password_resets, and now, we have create guitar table. And it gives us something to already begin with. It uses schema create, that is for creating a table. We specify the table name, and then there's a function that's going to execute, in order to create that table. So the first thing that our table needs is an ID. Every table needs an ID, it doesn't matter what it contains, it doesn't matter if you don't think it needs an ID, it needs an ID. So every table that you create should have an ID, and the reason why I say that is because there will be a day. When you say to yourself, I wish I put an ID on that table. So it doesn't hurt to put an id in every table. So we have increments. That means it is going to auto increment the value within this column, this id column with every new row that is our id. So then, there's the time stamps which is going to create several columns. I don't remember exactly what those columns are but we will see them here in a moment. Now, it's important to note that we have just created a migration. And all that does is create this file. If we look at our database utility, whenever you want to look at the guitars table with or the guitars database rather, you can see that we don't have any tables at all the migration did is create this file. So if we wanted to, we can come in here and we could delete this file. And everything is just fine. Because we haven't made any changes to out database. Of course, we lost our migration. So let's go ahead and recreate that. Now, you might get an error here, and if you do, then it's really easy to get around. It's not a level issue or anything like that. It's actually composure issue and you can run composure dump autoload and that should fix that, you'll be able to run the make migration command again and everything should be fine. But this case, it worked for me. So now, we can go back and look at our folder and viola, there it is, and it created the same exact code. So once again, we see Schema create and then we Schema drop if exists. So let's add some things here, we have the ID, which we definitely want. Let's also have the name of the guitar. We need to be able to store a string here. And we do that by using the string method. We specify the name of this particular column. We'll just call it name, and that will be it. Now, in other lessons, we will look at some of the other methods because we will be creating more tables that have more types of data. And so you will see more methods of creating other column types like integers and so on. So this is going to create our guitar table and actually, that should be guitars. So you know what? I want to be absolutely correct with this, so we're going to say, create guitars table. Now, once again, I want to note that all I'm saying is create guitars table, and then eloquent, or artisan, rather, is going to take that, it's going to see that it is creating something and it is going to create a migration for creating that table. So let's create the singular guitar so that we have the plural guitar, or guitars. Because when it comes to storing information within a database, you're storing multiple things. So it makes sense for your table name to be plural, that's not always the case, but in most cases it is. So once again, let's write table string and then name, and then we are good to go. So now, we want to actually perform this migration. And we do that with php artisan migrate. This is actually going to create those tables, and so whenever we look at, well we have an issue here. No connection could be made because the target machine actively refused it, and that indicates to me that there's an issue with my configuration. Now, another host, the database, the username and password are correct so the port is probably the issue, so let's look at what that is. 8889, so yeah. That would be an issue. So let's change that. We'll go back to the command prompt, and we will say, php artisan migrate. And I get another error, max key length is 767 bytes. Now, you might not get this error. And if you don't, that's fine. Now if you do, it's also fine, because you're using an older version of MySQL, which I happen to be doing. So we can easily get around this by going to the app folder, and then providers, and then app service provider. We need to add a use statement for Illuminate/ Support/ Facades and Schema. And then inside of the boot method we are going to say Schema and then there's a method called defaultstringlength and we're going to set that to 191. That will fix that. However, that migration did create something because you can see that there is a syntax error as opposed to the error that we saw before where no connection could be made. So this means that we actually have some tables here, but this is easy enough to fix we just need to drop those tables. So we can drop them. Yes, and then we'll go back and issue that migrate command so we can that it created the users table, the password resets table and the guitars table at least ran those migrations. It's going to keep track of what migration have been ran inside of this migrations table. So if we look at that, we can see that we have the migration and the batch. Now, I don't really know what the batch is but I assume that it is keeping track of which migrations were run within a given command. So we called the migrate command, and it created or it ran these migrations in that particular batch. And the next time that we run the command, it's going to have batch two. And then the third time it's gonna be batch three and so on and so forth. We'll just actually see if that is the case. But we also have our guitars table, if we look at that we have ID, name and then the created at and updated at. So if we look at the code, then those time stamps is creating those two fields, and, of course, there's also the tables for the users as well as the password resets. But just for fun let's go back to the command line, and let's say php artisan migrate. And then, we're going to say rollback. And that is going to roll back all of those changes. It'll show you the migrations that were there, so we had those three migrations within that batch. Those are now gone. So if we go back and look at our database, we don't have anything except the migrations table because that is keeping track of all our migrations. However, you can see that there's nothing there because we have no migrations ran because we rolled them back. But we wanted those tables so, let's go ahead and run the migrate command again. Once again, that command is ran. And if we look at the database, we have our tables. And so that is the basis of a migration. We use it to create our tables. We also use it to modify our tables, which we haven't done. However, we will do that in future lessons. And now that we have tables, we want at least some beginning information so that as we are developing our application we have something to work with. Well, that's called seeding. We want to seed our database with information, and we will do that in the next lesson.