Step 1: Create a New Database in PhpMyAdmin
-
Open phpMyAdmin: Navigate to your browser and go to http://localhost/phpmyadmin (or
http://yourdomain.dev/phpmyadmin
if you have set a custom domain). -
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.
-
Open the
.env
file in your Laravel project. -
Modify the following database configuration lines based on your database settings:
-
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, use127.0.0.1
orlocalhost
. -
DB_PORT
: The port MySQL is running on. The default is3306
. -
DB_DATABASE
: The name of the database you created in PhpMyAdmin. -
DB_USERNAME
: The username to connect to the database. For XAMPP, the default isroot
. -
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.
-
Open a terminal or command prompt.
-
Navigate to the root of your Laravel project (where
artisan
is located). -
Run the following command:
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.
-
Open phpMyAdmin again.
-
Select the database you created (e.g.,
testProject
). -
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:
-
Create a database in phpMyAdmin.
-
Update the
.env
file with the correct database connection details. -
Run migrations
php artisan migrate
to create the default tables. -
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!