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 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:
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.