Laravel 5.7 — Databases and Migrations

In order to connect a database, we will concentrate on the .env file. This is where you will find the configuration settings to set the database host, username and password along with a host of configurations. You may never end up using all of them. Below is an example of the .env file that is generated when you create a new Laravel project.

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:7b6e1+2Ok25kR2g54fVFeEWfXciimtt5eAvBSK7RpWc=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY=”${PUSHER_APP_KEY}”
MIX_PUSHER_APP_CLUSTER=”${PUSHER_APP_CLUSTER}”

The only part of the .env file we need to focus on is the MySQL configuration. We need to create the database using PHPMyAdmin or SQL Workbench. Then enter populate the following fields.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database-name-here
DB_USERNAME=username-here
DB_PASSWORD=your-password-here

There’s also a database.php file that can be found inside the config folder. This file shows you the different database connections you can make. For now, we will use the default MySQL connection. That is good enough. You can see the file here on the official Laravel Github repository: https://github.com/laravel/laravel/blob/master/config/database.php


Migrations

In order to create a migration in Laravel we use the following command;

php artisan migrate

Once you run this command it will create the users & password reset tables. By default, it also creates a migration table where it’ll store every single migration you make. You should never tinker with this table.

Here are the default migration files that come with Laravel out of the box.

Let’s have a quick look at the migration file and what it’s doing once the migrate command is run in our terminal.

So in the up function, this is where the fields are specified. It’s all pretty straightforward. When we run the migrate the command, the up function is executed and the table is created inside our database. If you’ve run the migrate command successfully, head over to the database that you’ve created, give it a refresh and take a look at the tables that have been created.

As you can see, the columns have been created which reflects the user migration file.

What if we want to delete the table? Can we do this through PHPMyAdmin or Workbench? Yes, we can do that, but if there is a command for it, why not use that? We must get into the habit of using the Artisan commands that are available to us, it will save you loads of time, trust me!

php artisan migrate:reset

The above command will reset the migration for you, there is also the rollback command that rolls back the LAST migration you ran. So that is why it’s important we don’t tinker with the migrations database and the files that are created in the migrations folder. We require these.

There is another command that we can also run, but it’s very important that you only use in the local environment and not on a live server/project.

php artisan migrate:fresh

The migrate:fresh command will drop all tables from the database and then execute the migrate command.


Creating a migration

To create a migration, we can use the make:migration command. This will create the PHP file inside our migrations folder.

php artisan make:migration create_tasks_table

We now have a migration file created, open up that file and let's start adding some fields to it. What I’m going to do is add a title and description field to our database. A task will have a title & a description.

Title and Description have been added to the migration file.
We now have a title and description field when we ran the migrate command.

If you want to make a change to the migration file without deleting the table, we can run the following command;

php artisan migrate:fresh

This will run ALL migrations again. You can find out more information on the Laravel documentation: https://laravel.com/docs/5.7/migrations#running-migrations

In the next article, I will show you how to insert data into the table and how we can query this in our Laravel project.

I hope you’ve enjoyed this article, give it a clap and share. If you have any comments, feel free to use the comments section or get in touch with me on my Twitter. Don’t forget to have a look at my previous articles;