Join Joseph Lowery for an in-depth discussion in this video Creating tables via migrations, part of Laravel 4 Essential Training.
- Eloquent ORM is a key aspect of Laravel's database integration solution. In this lesson, I'll show you an alternative approach to that of creating tables via Laravel routing, as we did in the last chapter. Building tables with Eloquent ORM and migrations gives you the flexibility to undo and restore your actions, definitely useful. Let me show you how it works. So to build a new table for our database via migration, we're going to turn to Artisan, Laravel's command-line interface.
I'll use Artisan to create our file for a table called "paintings". So I've CD'ed into my app directory, newapp, and I'll issue the command "php artisan migrate:make" as the function there and then give it the name of the file to make and I'm just going to call this "create-paintings". In just a second or two this will create a new file in my app database migrations folder, let's go take a look.
So, app, database, migrations, there's our new file. It's time and date stamped and then also given the name that we specified. I'll open that up and let me just expand the initial views columns here. You can see that there are a couple of libraries that it includes, blueprint and migration and then next, starting on line six, we extend migration, the basic class, and there are two functions, up and down.
Up is for moving forward, down is for moving backwards or undoing. So in the up function, I'll need to build a simple table with five columns. You'll find the code that I use very similar as you use when coding this directly in the route, home controller. The difference here is that we're making it easily reversible by including it in a migration. Ok, so let's start with our Schema statement, "Schema::create", and we're creating the table "paintings", very different from art, function, then we'll pass in the parameter "thePainting".
Now we'll begin to create our columns by entering in the variable, thePainting, followed by a pointer and then the first column, which is an ID column and will of course use increments to make an auto-incrementing column called "id". Next thePainting, we'll give a title for the painting then a similar column of type string for the artist name. This time I'm just going to put in a column called "year" for the year that it was created and we'll give that an integer type and then again we want to set this up for migrations so we'll use thePainting->timestamps() function.
Now that's the up function, that's when we first run the migration. Now down in the down function, we want to enter whatever would reverse that action. So that's a very simple command, it's just "Schema::drop" and then the name of the table, "paintings". Now notice that we're not checking for existence here as we did before, drop "if exists", we know it exists because we created it during the initial migration.
Ok, I'll save the file and the migration is done. Let's go back to the command line and execute. Command's pretty simple, "php artisan" and then just "migrate". Now you'll probably get a warning saying "Application In Production", don't worry, this is just Laravel asking for a little bit of confirmation. You can avoid this confirmation, if you like, by using the --force flag, if it really bugs you. I've gotten used to it so I'm just going to go ahead, I kind of like the confirmation myself, so I'll go ahead and press Y for yes and looks like our migration table was created successfully.
So let's take a look at it in phpMyAdmin to make sure that's the case. I'll hit refresh and there is my paintings table, take a look at the structure and everything looks good. So now that we've created it, let's uncreate it and roll back to the previous state. I'll switch to Terminal and type in "php artisan migrate:rollback" and hit enter.
Yes, I do want to run this command and there's our confirmation. Alright, let's double check that the table's gone. Going back to phpMyAdmin and I'll just refresh the page and when I look at newapp, my paintings table is no longer. Now, you may be wondering "how do you undo a rollback?" Well, that's easy. Go back to Terminal and again type "php artisan migrate" no rollback, just migrate again.
Yes to confirm and it says we've created it. Let's verify, phpMyAdmin, I'll refresh the page and there's our paintings table. Well that's great for table creation but what about updates? Basically, same process. Let's go to Terminal and I'll create a new migration file. "php artisan migrate:make" and I'll call this one update paintings, "update_paintings".
It'll take just a moment and once it's done, let's go to the code editor and there's my update_paintings file, I'll open that up and in the up function, I'll put in a new Schema table command, "Schema::table" and the first of my argument is the table I want to address and it's "paintings" and then "function" and I'm going to call this newcolumn, and I'll enter in "newcolumn->string" and the type of field that I want and this will be an email field, put in my requisite semicolons, looks like I'm error free there and now let's go down to the down function and that's the opposite.
"Schema::table" again addressing "paintings", again function, newcolumn and we want our new column command to be "dropcolumn" and which column are we going to drop, email of course. ;; alright, save the file and let's run that migration. I'm just going to use my up arrow key to go to "php artisan migrate", confirm and my update paintings has been migrated.
One last time, let's head back to phpMyAdmin. I'll refresh the page and now I have an email field and now that we have the email field, let's roll it back and get rid of it. So, back to Terminal, "php artisan migrate:rollback". Ok, rolled back our update_paintings, one final conformation, the refresh of the page, email's gone. Perfect.
So now you have a programmatic method for not only creating database tables, but also for handling updates in a systematic way that allows for version control and rolling back those updates should anything go wrong.
Need a quick dive into Laravel? Check out this short primer, Up and Running with Laravel.
- Installing Laravel and Composer
- Routing requests
- Filtering routes
- Incorporating advanced controllers
- Creating a basic Blade template
- Developing a layout with child pages and forms
- Integrating a database
- Creating tables via migrations
- Outputting data
- Building a Laravel app
- Authenticating users
- Deploying Laravel code