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.
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.
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
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.
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
migrate:fresh command will drop all tables from the database and then execute the
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.
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;