How To connect Laravel Application to MySQL Database.

Introduction

Here is a simple guide on establishing a connection between your database(MySQL) and Laravel application.

Step 1 : Creating the database

This can be done either by a database manager eg phpMyAdmin or the Command Line…



Introduction

Here is a simple guide on establishing a connection between your database(MySQL) and Laravel application.



Step 1 : Creating the database

This can be done either by a database manager eg phpMyAdmin or the Command Line Interface (CLI).

  • Database Manager(phpMyAdmin)
    Creating a database on phpMyAdmin

  • Command Line Interface(CLI)
    A database can be created with the following command:

CREATE DATABASE db_name;

P.S: You have to log in to MySQL before you can create a database via any of these methods.



Step 2: Update .env configurations

Laravel default environment file .env contain some common configuration values that differ based on if your application runs locally or on a production server.

P.S: This configuration values are based on my local machine ,
yours may differ, do well to confirm.

Update the .env with the database name you want to connect to,as well as the other environment variables if they differ from the default.

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

__Hurray! ?The laravel application is now properly connected to the database.



Step 3: Migrations(optional)

Laravel migrations simply allows you to easily perform certain actions to the database without going to the database manager (eg. phpMyAdmin). They can also serve as a version control for your database. They create tables, columns as well as modify them etc.

Migrations are found in database/migrations directory.
Laravel application comes default user migration ,it can be executed with the following command:

php artisan migrate

P.S : Refresh your database , it has been updated with users table and its properties.

I wrote an article to explain more on migrations here. You can also visit the laravel documentation here

You’re all set to continue building!?
Thank you for reading?


Print Share Comment Cite Upload Translate
APA
Funke Olasupo | Sciencx (2024-03-28T23:25:03+00:00) » How To connect Laravel Application to MySQL Database.. Retrieved from https://www.scien.cx/2021/06/06/how-to-connect-laravel-application-to-mysql-database/.
MLA
" » How To connect Laravel Application to MySQL Database.." Funke Olasupo | Sciencx - Sunday June 6, 2021, https://www.scien.cx/2021/06/06/how-to-connect-laravel-application-to-mysql-database/
HARVARD
Funke Olasupo | Sciencx Sunday June 6, 2021 » How To connect Laravel Application to MySQL Database.., viewed 2024-03-28T23:25:03+00:00,<https://www.scien.cx/2021/06/06/how-to-connect-laravel-application-to-mysql-database/>
VANCOUVER
Funke Olasupo | Sciencx - » How To connect Laravel Application to MySQL Database.. [Internet]. [Accessed 2024-03-28T23:25:03+00:00]. Available from: https://www.scien.cx/2021/06/06/how-to-connect-laravel-application-to-mysql-database/
CHICAGO
" » How To connect Laravel Application to MySQL Database.." Funke Olasupo | Sciencx - Accessed 2024-03-28T23:25:03+00:00. https://www.scien.cx/2021/06/06/how-to-connect-laravel-application-to-mysql-database/
IEEE
" » How To connect Laravel Application to MySQL Database.." Funke Olasupo | Sciencx [Online]. Available: https://www.scien.cx/2021/06/06/how-to-connect-laravel-application-to-mysql-database/. [Accessed: 2024-03-28T23:25:03+00:00]
rf:citation
» How To connect Laravel Application to MySQL Database. | Funke Olasupo | Sciencx | https://www.scien.cx/2021/06/06/how-to-connect-laravel-application-to-mysql-database/ | 2024-03-28T23:25:03+00:00
https://github.com/addpipe/simple-recorderjs-demo