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.
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.
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.
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.
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.
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.
Check out your new tables
Taking a trip back to our MySql console shows us the new tables in all of their glory.
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); } } |
Here is the code that created the Password Resets table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePasswordResetsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('password_resets', function (Blueprint $table) { $table->string('email')->index(); $table->string('token'); $table->timestamp('created_at')->nullable(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('password_resets'); } } |
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
--- ip: "192.168.10.10" memory: 2048 cpus: 1 provider: vmware_workstation authorize: ~/.ssh/id_rsa.pub keys: - ~/.ssh/id_rsa folders: - map: C:/localdev to: /home/vagrant/Code sites: - map: phpmyadmin.dev to: /home/vagrant/Code/phpmyadmin - map: homestead.app to: /home/vagrant/Code/Laravel/public - map: 54.dev to: /home/vagrant/Code/54/public databases: - homestead # blackfire: # - id: foo # token: bar # client-id: foo # client-token: bar # ports: # - send: 50000 # to: 5000 # - send: 7777 # to: 777 # protocol: udp |
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.
1 2 3 |
192.168.10.10 phpmyadmin.dev 192.168.10.10 homestead.app 192.168.10.10 54.dev |
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.
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.