|

Getting Your Database Set Up For Use With Laravel

set up laravel database

Now that we’ve covered the most simple introduction to Laravel, let’s take a moment to get ready to work with a database so we can make use of the Query Builder or Eloquent. You have many options to choose from such as sqlite, pgsql, redis, and mysql. MySql is far and away the most used option, so that is what we’ll look at here. In addition, we can work with the database via the command line, or from a graphical user interface such as phpMyAdmin. We’ll take a look at both options in this tutorial.


Keeping Your Project Up To Date

Laravel moves fast, and if you want to keep up to date with things, make it a practice to run a composer update from your project root every so often. Thanks to Composer, keeping things up to date in your project is so simple. Here we update our most recent practice repository.
Keeping Your Project Up To Date


SSH to the virtual machine

We are using Homestead in this tutorial, so take the steps to get that installed if you would like to follow along. Here, we SSH into the vitual machine to get ready to work with MySql.
SSH to the virtual machine


Log in to mysql

Once you are logged in to the VM, you can easily go to the MySQL command line by simply typing mysql and hitting enter.
Log in to mysql


Create a new database to work with

Let’s go ahead and first look at what is in the database by typing show databases;. We see a few of them such as information_schema, homestead, mysql, performance_schema, and sys. We want to create a custom database for this so we type create database '54'; and note that we get a result of Query OK, 1 row affected. If we show the databases again we see our new database.
Create a new database to work with


Set up the env file

Now that we have a specific database we want to use for our project, we can set the variables we need in the .env file. By default, the DB_DATABASE key is set to homestead, but we will change it to 54 for this example. We leave the
DB_USERNAME and DB_PASSWORD fields at their defaults.
Set up the env file


Migrate the database

Migrations are a way of defining the Schema of database tables using PHP. When we run the command of php artisan migrate, this will actually trigger those built in PHP files to build a couple of helpful tables in our database. As we can see below, when we run it, we do get a users as well as a password_resets table. Almost all web applications make use of something like this.
migrate the database


Check out your new tables

Taking a trip back to our MySql console shows us the new tables in all of their glory.
New tables have been created


What code created our new tables?

If you’re curious, here are the PHP files that built out those tables for us.
Here is the code that created your Users Table

Here is the code that created the Password Resets table.


Database setup is complete!

Laravel is now connected to and working with your local database. We now have all the plumbing set up and configured for working with MySql.


Get a GUI for your database

Sometimes, working with the command line interface for the database is a little clunky. It’s easy to add phpmyadmin to Laravel homestead so let’s do that now.


Use Composer To Install phpMyAdmin

In your terminal, visit the directory on the host machine, which is shared with the virtual machine. Run the following command.

composer create-project phpmyadmin/phpmyadmin –repository-url=https://www.phpmyadmin.net/packages.json –no-dev

In our case the shared directory is C:/localdev, so what this will do is create a folder named phpmyadmin and install all of the software in that directory.
install phpmyadmin using composer on laravel homestead


Update Homestead.yaml file

We are actually installing phpMyAdmin as a new site in homestead, so let’s go ahead and update the settings in our Homestead.yaml file to reflect this. In this file below we have the default homestead.app site, 54.dev, and now we are adding the phpmyadmin.dev site as well.


Update the hosts file

You’ll need to update the hosts file found at C:\Windows\System32\drivers\etc on a Windows machine using administrator privileges to something like the following. This allows us to use these easy domain names in our browswer during local development.


Run vagrant provision

We’re almost done, but before the GUI becomes active, we need to run a vagrant provision command in the homestead directory of our local machine. This will add our new phpmyadmin.dev domain to the available sites.


Try out your new phpMyAdmin Installation

Oh yeah! Look at that database with those tables that were created during the migration step we did earlier. Everything is working great. So we have Laravel configured, MySql configured, and now we also have a nice GUI to work with by way of phpMyAdmin. Things are looking good.
phpmyadmin is now installed on Laravel homestead


Getting Your Database Set Up For Use With Laravel Summary

This was a nice little tutorial covering some of the basic steps to take when getting ready to use Laravel with MySql as a database. We learned a little bit about how to log into MySql on the virtual machine, how to show and create databases from the command line, how to configure the .env file supplied with Laravel, how to run migrations in Laravel, and how to use composer to install the latest version of phpMyAdmin for use as a graphical user interface on our database.