Database Setup Laravel 5.1

Access all tutorials in sprocket icon.

June 14, 2015 from author Bill Keck.

Beginner Tutorial for setting up your database connection to your Laravel 5.1 application.

Step 1. Create the Database

If you are following along with my beginner’s development environment, we are using MAMP, which comes with a MySql build that is ready to use. It also has PhpMyAdmin implemented, so you easily interact with the database.

You are not obligated to use this development environment, but my instructions will be for this type of setup.

So to get to the PhpMyAdmin interface, open the MAMP application and click on Open WebStart page. Next you’ll see:

MySQL

MySQL can be administered with phpMyAdmin.

Click the phpMyAdmin link, and this will pull up the phpMyAdmin page. Select the Databases tab, and below that you will see an input field for database name and a dropdown list for collation.

I typically name the DB the same name as the project, all lowercase. The collation we use is utf8_unicode_ci. We use that collation because it supports proper sorting in multiple languages. After you select your collation, press the create button, and your database will be created for you.

Step 2. Set the Password.

One slight complication I ran into is that it’s a little tricky to set a password. You can leave it as is and use an empty string in your Laravel config, but that’s not what I did.

I opened the following:

Applications/MAMP/bin/phpMyAdmin/config.inc.php and edited config.inc.php with:



$cfg['Servers'][$i]['password']      = 'mypassword'; 

Obviously use your password where it says mypassword. There’s probably an easier way to do this, so Google that for yourself or leave a comment if you want to share. It’s good to know how to set it through config.inc.php if you ever happen to lock yourself out.

Step 3. Set DB parameters in .ENV in your Laravel app.

Once you are on the Laravel side of things, setup is a snap. Open your .ENV file and set the following:



DB_HOST=localhost
DB_DATABASE=yourDBname
DB_USERNAME=root
DB_PASSWORD=mypassword

Obviously fill in yourDBname and mypassword with your values.

By default, Laravel 5.1 is configured for mysql as the default DB. If you need to change that, you can go your Laravel config folder and open database.php. You can see it’s a settings array and the default key is:



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

Step 4. Run the Migration

Laravel 5.1 comes with a couple of migrations to setup the users table and the password_resets table. It will also create a migrations table to keep track of all the migrations.

You can see the migrations in database/migrations folder. You will see two files:

2014_10_12_000000_create_users_table.php
2014_10_12_100000_create_password_resests_table.php

These are pre-configured migrations, so you don’t need to change anything. If you are new to migrations , they are a set of instructions to the DB from a php file to do things like create tables and columns. You can also use them to create indexes and foreign keys. There’s also a rollback method, in case you need do undo something.

The laravel docs on migration are a great resource.

Once you run the migrations from the command line, it will create the tables you need to add users and manage password recovery.

To run the migration, go to your command line and run:



 php artisan migrate

Once you are done, check via PhpMyAdmin to make sure the tables were created. Now that you have your DB created and communicating with Laravel, you will be ready to move on to creating your user registration and login.

I hope you have enjoyed this tutorial and found it useful. Click on the sprocket icon at the top of the page to see all tutorials. Please comment, share, and like if you can, thanks.

I don’t have a donate button, but If you would like to support my work and learn more about Laravel, you can do so by buying one of my books, Laraboot: laravel 5* For Beginners, I really appreciate it.

6 thoughts on “Database Setup Laravel 5.1

  1. Venkat says:

    Sir, Your Tutorial is Awesome… The way you Explained topics is Understandable… Can You Explain the Request Folder in Laravel, Why we use it??? Thanks In Advance

    Like

    • Thanks for the kind words. Sometimes the validation rules can be complex or sometimes you just want to extract it out of the controller. In this case, you can create a custom Request class for this purpose. So real quick, you would create one of the classes by running an artisan command: php artisan make:request ExampleRequest

      This would create an ExampleRequest file in the Requests folder, which you could then add your custom requirements to the rules array. Then in your ExampleController, you could type hint ExampleRequest instead of Request in the create and edit methods and it will instantiate it for you. It’s very easy to use once you understand it. I will do a tutorial on this soon.

      Like

  2. Yours is the first page I’ve come across that had my answer (out of maybe 20-30). Either I simply didn’t know how to search for what I was looking for, or it’s just not well-documented. I’m leaning towards the former as I don’t really have the search game I used to.

    * If the tables ‘users’ or ‘password_resets’ tables are missing, the defaults can be restored by placing their respective migrations into the database/migration folder and running: php artisan migrate
    2014_10_12_000000_create_users_table.php
    2014_10_12_100000_create_password_resests_table.php

    I found these 2 files in another Laravel install I had setup in another test folder. So you can just create a new Laravel project and grab them. It’s kind of nice having a backup of all the original files anyways, especially for us Laravel noobs🙂

    Thank you for the reference to those migration files!

    Like

  3. prateekvarma says:

    Hello,
    on running “php artisan migrate”

    i get
    “[PDOException]
    SQLSTATE[HY000] [1045] Access denied for user ‘homestead’@’localhost’ (usin
    g password: YES)”

    Could you tell me where am i going wrong?
    Thanks

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s