Join Joseph Lowery for an in-depth discussion in this video Creating tables with Schema Builder, part of Laravel 4 Essential Training.
- Now that we've established our database connection in Laravel, let's create a table with that database. Of course, you could do this through phpMyAdmin or the command line, for that matter, but you do gain a tremendous amount of flexibility by building your database structure programmatically. Another term for database structure is schema, and we'll be applying Laravel's Schema Builder functionality to accomplish our task. You want to be able to make sure that your database table is created the first time anyone accesses your application.
So, Schema Builder functions typically go in the root.php file, or if you're using controllers, as we are, the home controller file. So, let me open that up. And I'm going to put this in the public function showWelcome. First I'll create a little bit of space here, and now we're going to frame the entire code block within a call to the Schema class, followed by the create method, and then we'll pass two arguments.
The first is the name of the table, to be put in quotes, that we're going to create, and here it'll be art, and then our closure parameter. The closure accepts just one variable as its argument, which will be used to hold the table Schema that we're about to define. So, I'm going to put it in as $newtable and then enter curly braces. Let's go ahead and put in that closing semicolon there, and now we're ready to begin specifying the Schema.
Now, most databases have a unique identifier or ID for each entry. So, we'll make that our first column. Within the curly braces, enter the closure variable $newtable, followed by the pointer, and then the column type. In this case, in Schema Builder, the auto-incrementing column type is called increments, and I'll follow that with a pair of parentheses and single quotes, and within that, the name of the column, id, and then close it all off with a semicolon.
All right, so that's kind of the pattern, the basic structure, if you will, for creating all the columns within the table. Let's do a few more, so I can show you which column types are typically used. Of course, a good number of columns will just hold strings. So, we'll put in $newtable, string, and then we'll call this first one, artist. A string column type creates a varchar field, the default value of which is 255 characters, plenty for our artist's name.
You can also specify the number of characters, if you want to, in a second parameter. Let's make one for the title of the paintings. Again, it's a string, and we'll call this title, and the second parameter is 500, for the number of acceptable characters. All right? I'm going to move that over, because that's bugging me. There we go. Much better, don't you think? All right. Now, for longer entries use the text column type, and this will be our description.
We can also include dates, like when the artwork was created. So, that is just date, and I'm going to call that created. Another date might be the exhibition date, when the first exhibition is scheduled. Again date, and I'll do exhibition_date. Finally, we're going to insert a special Laravel column type called timestamps.
Timestamps creates two columns, one to keep track of when the record was originally entered and another when it was last updated. We're going to use these later in the course, when discussing Laravel migrations. Timestamps doesn't take any arguments. All right. Let's take a look and see what we've got. I'm going to go ahead and save the file, and then in the browser switch over. Open up a new tab. So, I still have my Artisan web server running. So, I'll now go there by going to localhost 8000, and looks like everything ran okay, no errors.
Let's check it out in phpMyAdmin. I'm going to go ahead and hit the refresh page here, and it looks like our art table was created. Let me hit the structure, and it looks like everything is there, including the two columns, created at and updated at, that were set up by our inclusion of the timestamps. Perfect. Well, now that our table structure is created, we're going to need to remove the code. So, I'm going to go back to here, and I'm just going to comment it out, so you'll have it in the exercise files to refer to.
All right. We'll take a look at how you can modify your tables once they're created using Schema Builder, coming up.
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