Connecting your Project to Database in Laravel

Connecting your Project to Database in Laravel

Connecting your Project to Database in Laravel


Connecting your Laravel project to a MySQL database or any other relational database is fairly easy.

Since we have done the installation with XAMPP. It comes along with already installed MySQL and phpmyadmin. In this tutorial, I will make use of phpMyAdmin to create a new database that will connect to the Laravel application.

  • This post is assuming you already have a Laravel project setup in your local and you are able to access its homepage via localhost or custom set domain name.

Step 1: Create a new database via PhpMyAdmin

Navigate to yourdomain.dev/phpmyadmin , Click on the Databases tab and create a new database with your desired name.

 

Clicking on create will create a new database in your XAMPP MySQL.

Step 2: Changes in .env configuration file.

Once the database is created, you need to tell your Laravel project the details about the database.

Laravel version 5 has a pretty easy way to do that, All your configurations that you should keep private and should not share go into the .env file of your project.

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

Modify the following property in your .env file according to your database settings.

Step 3: Run migration (optional)

To test if you have successfully connected your Laravel project to the database you can run the migration command. Laravel project by default comes with some default tables for storing users and their password requests. When you run migrate command you it should create the default tables for you in the database.

php artisan migrate

That’s it! You are now ready to make database operations in your project.

Reactions

Post a Comment

0 Comments

close