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.
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:
…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:
If you have already learned about routes, you know from the above code to go to:
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:
||Incrementing ID to the table (primary key).|
||Incrementing ID using a “big integer” equivalent.|
||VARCHAR equivalent column|
||VARCHAR equivalent with a length|
||INTEGER equivalent to the table|
||BIGINT equivalent to the table|
||SMALLINT equivalent to the table|
||FLOAT equivalent to the table|
||DECIMAL equivalent with a precision and scale|
||BOOLEAN equivalent to the table|
||DATE equivalent to the table|
||DATETIME equivalent to the table|
||TIME equivalent to the table|
||TIMESTAMP equivalent to the table|
||Adds created_at and updated_at columns|
||Adds deleted_at column for soft deletes|
||TEXT equivalent to the table|
||BLOB equivalent to the table|
||ENUM equivalent to the table|
||Designate that the column allows NULL values|
||Declare a default value for a column|
||Set INTEGER to UNSIGNED|
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!