November 2, 2015 9:43 pm

Setting up your database and tables in Laravel 5

In this article, we will show you how to set up your database, use migrations in Laravel 5 and get help for artisan commands.
Setting up your database and tables in Laravel 5

Setting up the database

Once you have Composer set up and have created a Laravel project in some folder you would have to set up your database and tables. You can create a new Laravel project with Composer by typing composer create-project laravel/laravel in your Terminal/Command Prompt after having navigated to the folder where you want Laravel to be set up (You would need Composer set up in your PATH environment variable to use it from any folder in your Terminal).

Next, you would need to open the .env file which is located in the root of the project. Inside that file, you would need to set up your environment variables related to your database.

Those are my database settings in the .env file:

DB_HOST=localhost
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=*****

You can also check the config/database.php file and see or change the default settings related to your database. The default settings will be implemented if no .env file exists or no such property exists in it. For example, when you have the following lines in the database config file

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),

The host would be set to the DB_HOST property that you set in your .env file. If no such property exists in there, then the value localhost would be used.

'default' => env('DB_CONNECTION', 'mysql'),

If you cannot find a DB_CONNECTION property in the .env file and you see the line above, then your application is using a MySQL database. Therefore, if you want to use SQLite instead or a different database you could either add a new property named DB_CONNECTION=sqlite in your .env file or just change mysql in config/database.php.

You should also set the public folder to be the landing root for the web server.

Migrations

You do not have to manually create your table. Start a migration for the new table by typing php artisan make:migration create_blog_table –create=blog in your Terminal. This will create a template for creating a new table called blog. You can find that migration file inside the database/migrations folder.

Below is the migration file that resulted from our command. The only thing we have added is a string (VARCHAR) field which we called title and a text (TEXT) which we called content.

The $table->timestamps method adds the timestamps (created_at and updated_at) which Laravel manages by itself. Whenever you add a new item or update an existing one in your database, Laravel will update those timestamps.

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBlogTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('blog', function (Blueprint $table) {
            $table->increments('id');
            $table->string("title");
            $table->text("content");
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('blog');
    }
}

Now, all we have to do is type php artisan migrate in our Terminal and the table with all entered fields will be created.

Actually, the up method is the action that you want to perform when implementing the migration and down is the method that you want to implement when reverting the change brought by the migration.

Therefore, if you create a new table within the up method you would drop the table in the down method. Similarly, if you add a column in the up method, you remove the column in the down method.

You can return your database to its state prior to the last migration by typing the command php artisan migrate:rollback. The command will execute the down method for the last migration.

Help in learning Artisan commands

Artisan provides a lot of help commands. You can use php artisan list to see all currently available commands. You can type php artisan help commandName to see what kind of arguments a command accepts, how you can use the command and what it does.

The image below provides an example of getting help for rollbacks of migrations:

example of getting help for rollbacks of migrations

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter


Tutorial Categories:

2 responses to “Setting up your database and tables in Laravel 5”

  1. Wiksiloh says:

    Thanks for this tutorial… I had problem in connecting to the database buh now av been able to connect to my database… tnx

  2. Wiksiloh says:

    Thanks for this tutorial… I had problem in connecting to the database buh now av been able to connect to my database… tnx

Leave a Reply

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