Creating Database Tables with Laravel

Okay, today we are going to cover how to get started with Laravel Database tables. You can create databases with Laravel, but for this tutorial on creating tables with Laravel, we are going to simply use phpmyadmin to make our database if you don’t already have one made for your Laravel project.

Prerequisites

If you have not yet even installed Laravel, see my first tutorial on Laravel, Installing Laravel on XAMPP at http://jafty.com/blog/installing-laravel-php-framework-on-xampp/

That’s really all you have to do before using this getting started tutorial, but you may need to reference other tutorials as you go depending on what you already know. I will try to reference the proper ones throughout this tutorial series to help you learn.

Selecting a Database for Laravel

As you probably are aware, most web sites need a database, so the first logical step to getting your Laravel project off the ground after installing the Laravel PHP framework itself, is to add a database. I am going to instruct you on how to install a MySQL database with Laravel. If you want to use a different database or storage option….. well, you are on your own. Sorry, but I don’t know why you would want to use anything other than the most popular free developer’s database available, MySQL. How do I know? You may have a legitimate reason, unfortunately, I just can’t help you, but Laravel can, they have an abstraction layer that makes the syntax for all database and storage methods the exact same. No matter what database you use, you will be okay, but some of the steps in this particular tutorial will be slightly different, but don’t worry too much about that, they should be obvious if you have enough experience with your database of choice which you should if you are ready to jump into learning a whole new PHP framework.

Configuring a Laravel Database

Now you are ready to make changes to the config files for Laravel to accommodate your database. Find the config file at app/config/database.php. If you are using a localhost install of Laravel for learning purposes, which is probably smart, then your config file changes will be minimal really. All you have to do is change the name of the database in the “database” variable at or about line number 57(depending on how many edits you made and what version of Laravel you are using). Change the default value of ‘database’ to the name of your database(unless you named your database, ‘database’). I named my database ‘laravel_test’ for this tutorial. The database will be the main database for your project and will hold all of the project’s tables.

Create a Laravel Database

Now you have to go to your PHPMyAdmin url and create the actual database. If you are developing on the localhost, you can go to localhost/phpmyadmin or wherever you installed phpmyadmin and create a database named after the same name you set in your config file for the ‘database’ option in the database.php config file. In my case, I named my database ‘laravel_test’ and created that same database from localhost/phpmyadmin.

Creating a Database Table in Laravel

Okay, now that you have created a database, it’s time to really get to the meat of this tutorial and execute some code in Laravel to make a table on your new database.

Okay, remember I promised to tell you if you might need another tutorial to understand a part of this one? Here is the first case this has come up. You may wish to take a break from this tutorial now and check out my tutorial on Laravel Routes at:

http://jafty.com/blog/laravel-routes/

…or you may want to assume you can figure them out from the example in this tutorial, but you won’t get the whole idea of routes from this tutorial as it only uses routes to make a database table and that is not what they are really all about. Routes are actual routes to your different parts of your Laravel project, hence the name, “routes”.

So, whether or not you decided to take my advice and visit my “routes” tutorial, we are moving on! Here is the code you need to add to routes.php in order to create a database table using Laravel. as you can see, it’s a lot simpler than creating a table in .sql and a bit faster than creating one in phpmyadmin if you use it correctly:

Route::get(‘make_table’, function()
{
Schema::create(‘users’, function($table)
{
$table->increments(‘id’);
$table->string(‘username’, 32);
$table->string(’email’, 320);
$table->string(‘password’, 60);
$table->timestamps();
});
});

If you have already learned about routes, you know from the above code to go to:

http://localhost/laravel/project1/public/make_table

in my case to execute the above code. Your path will depend on how you installed Laravel of course, but it should be similar if you are following my tutorials. You won’t see anything but a blank window open when going to the URL. To check your work, go to phpmyadmin and examine the users table inside the database you created. It should have all the fields from the code above, but note that the timestamps() function actually creates two columns in the table, created_at and updated_at. So you should have six fields in your table, not five as you may have expected with only five lines of code.

Different Types of Data Fields

As you may know there are several types of data fields in Mysql, such as varchar, increment, bigint, etc just to name a few. Here is a list of Laravel commands for each so you can create the necessary fields of data for your own table:

Command Description
$table->increments('id'); Incrementing ID to the table (primary key).
$table->bigIncrements('id'); Incrementing ID using a “big integer” equivalent.
$table->string('email'); VARCHAR equivalent column
$table->string('name', 100); VARCHAR equivalent with a length
$table->integer('votes'); INTEGER equivalent to the table
$table->bigInteger('votes'); BIGINT equivalent to the table
$table->smallInteger('votes'); SMALLINT equivalent to the table
$table->float('amount'); FLOAT equivalent to the table
$table->decimal('amount', 5, 2); DECIMAL equivalent with a precision and scale
$table->boolean('confirmed'); BOOLEAN equivalent to the table
$table->date('created_at'); DATE equivalent to the table
$table->dateTime('created_at'); DATETIME equivalent to the table
$table->time('sunrise'); TIME equivalent to the table
$table->timestamp('added_on'); TIMESTAMP equivalent to the table
$table->timestamps(); Adds created_at and updated_at columns
$table->softDeletes(); Adds deleted_at column for soft deletes
$table->text('description'); TEXT equivalent to the table
$table->binary('data'); BLOB equivalent to the table
$table->enum('choices', array('foo', 'bar')); ENUM equivalent to the table
->nullable() Designate that the column allows NULL values
->default($value) Declare a default value for a column
->unsigned() Set INTEGER to UNSIGNED

Summary

That is it, you know now how to create a table using Laravel and routes! congratulations!

To learn more, see all my Laravel tutorials by Clicking HERE!

 

 

 

One Reply to “Creating Database Tables with Laravel”

Leave a Reply

Your email address will not be published. Required fields are marked *