9 things I've discovered so far while working with MySQL in Laravel.

Of all the posts I've written so far, I have never referenced any so much as the one about my database migrations, which was essentially a step-by-step instruction guide I wrote for my future self. (now y past self)

This post carries on from that and answers many of the questions I've had so far around the topic of interacting with databases in a Laravel application.

Some of this is laravel specific, and some are more related to general working with MySQL.

1 Multiple Laravel database support

MySQL may be the default but Laravel supports multiple databases, and if I want to change them, I need to specify it in the dot .env file. I've not personally tried all of these DB options. They are just those I'm aware of and searched for.

Support also may vary by version.

  • MySQL

  • SQL Server

  • SQLite

  • PostgreSQL

  • MariaDB

  • MongoDB

//inside the env file line 11

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=dogtracker
DB_USERNAME=root
DB_PASSWORD=

Bonus, why use an alternative to MySql at all

In a nutshell, each of the above has its own advantages. It depends on what suits your application.

MongoDB prioritises speed and can be a great choice for applications that require high scalability and availability, as it has built-in scalability and replication features.

MariaDB is an open source fork of MySQL that is well suited for use in cloud applications.

PostgreSQL offers advanced SQL features like row-level locking, complex queries, foreign keys etc.

SQLite is a lightweight library that can be embedded into applications without requiring any additional software or configuration.

SQL Server is a robust database platform with support for enterprise features such as business intelligence and analytics capabilities.

2 Automatic database naming

When creating a new Laravel project, it will automatically name the database to

The code example in the past point covers this one, too.

I named my new project dogtracker, which means that Laravel out of the box expects to work with a MySQL database named dogtracker.

This tripped me up a few times in other projects until I discovered I could change the name in the .env file.

Eg I called my project AwardsSystem so guess the database name.
In stead I called my new database awards and my migration was award.
A lesson in bad practice.

3 Databases can be created in the terminal

I need to ensure MySQL is installed on my machine. I can then enter MySQL as set out in my Migrations article where I can simply specify the name of the new database and then exit MySQL.

mysql -uroot -p

4 Naming conventions

There are naming conventions I should stick to when creating my migration name; there are naming conventions for everything. naming conventions is practically on the same scale as learning a whole new language.

The Naming Convention in question complete with its artisand command includes.

a make:migration create_awards_table

Notice the start with create and _ snake casing.

5 There are multiple options for interacting with existing Databases

Once my database exists, I can interact with that database in multiple ways.

Via Terminal

With MySQL installed, I can enter Mysql and access my databases directly in my terminal with commands such as:

use dogtracker
This is an instruction on what database to use in this case dogtracker.

show dogtracker
This is an instruction to show the tables available in dogtracker database.

I can take this much further and add or delete individual records in these tables using the SQL language.

Via a MySQL GUI

Specific sportware such as TablePlus allows the connection to and management of all tables within databases and all records within.

The GUI allows me to

Via an IDE

An IDE like PHP Storm can act as the best of both worlds allowing a visual interface and incorporating a terminal interface too. I have to admit I prefer the visual interface in Table Plus over storm but in practice I may just like the convenience of it better.

6 Use Laravel-specific commands to create database migrations

Database migrations are a way to version control the database structure. They allow developers to define changes they have made to the database in text files that can then be used to add, edit or remove tables and columns.

This acts as a blueprint so when you create a migration and then run a migration you still have

In Laravel, these migrations are stored in the application and can be executed with artisan commands from the terminal. This makes it easy to keep track of changes made to the database structure and revert any changes you don’t want.

7 Several Migrations already exist

Laravel comes with a bunch of migrations already included in the installation. It contains all the tables and columns necessary to build and manage user authentication and authorization, including users, passwords, roles, permissions, as well as various other features like password reminders.

This means you don’t have to manually create these tables yourself as Laravel does it for you. You can then start building your own custom migrations on top of these existing ones.

The built-in migrations ensure that the database structure is consistent across many different Laravel installations.

8 Seeding populates tables

Seeding in Laravel is the process of populating a database table with initial data. This allows you to quickly and easily set up an application with necessary data for testing purposes.

For example, if you're creating a CRM, you may want to create sample contacts, companies, tasks and notes to give users a sense of how to use your app. Seeding helps set up these initial records quickly and easily without manually entering them into the database by hand.