Connecting your Project to Database in Laravel

Connecting your Project to Database in Laravel

Step 1: Create a New Database in PhpMyAdmin

  1. Open phpMyAdmin: Navigate to your browser and go to http://localhost/phpmyadmin (or http://yourdomain.dev/phpmyadmin if you have set a custom domain).

  2. Create a Database:

    • Click on the Databases tab at the top.

    • In the Create Database section, enter the name of your new database (e.g., testProject).

    • Click Create to create the database.

Now, you have a new database that will be used by your Laravel application.

Step 2: Update the .env File

Laravel uses the .env file to store environment configurations such as database settings. After creating your database, you need to update the .env file in the root of your Laravel project.

  1. Open the .env file in your Laravel project.

  2. Modify the following database configuration lines based on your database settings:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=testProject # Use your database name here DB_USERNAME=root # Use the MySQL username (default: root) DB_PASSWORD= # Use the MySQL password (default: empty for XAMPP)
  • DB_CONNECTION: The type of database you're using (in this case, mysql).

  • DB_HOST: The hostname where the database is located. For a local server, use 127.0.0.1 or localhost.

  • DB_PORT: The port MySQL is running on. The default is 3306.

  • DB_DATABASE: The name of the database you created in PhpMyAdmin.

  • DB_USERNAME: The username to connect to the database. For XAMPP, the default is root.

  • DB_PASSWORD: The password for the database. For XAMPP, this is usually blank (empty string).

Step 3: Run Database Migrations (Optional)

To test if the connection is successful and ensure that Laravel can create the necessary tables in your database, you can run the migration command. This command will create default tables such as users, password_resets, and more.

  1. Open a terminal or command prompt.

  2. Navigate to the root of your Laravel project (where artisan is located).

  3. Run the following command:

php artisan migrate

This will run the default migrations that come with Laravel and create tables in the database you specified in the .env file.

Step 4: Verify the Connection

If everything is set up correctly, the migration should execute successfully and create the required tables in your MySQL database.

  1. Open phpMyAdmin again.

  2. Select the database you created (e.g., testProject).

  3. You should now see the tables created by Laravel, such as users, migrations, etc.

If the migration runs without errors, your Laravel application is successfully connected to the MySQL database.

Summary:

  1. Create a database in phpMyAdmin.

  2. Update the .env file with the correct database connection details.

  3. Run migrations  php artisan migrate to create the default tables.

  4. Verify the connection by checking the tables in phpMyAdmin.

That's it! Your Laravel application is now connected to a MySQL database, and you're ready to start working with database operations such as CRUD (Create, Read, Update, Delete). Let me know if you need help with any other steps!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close