How to connect mysql database using laravel?

by edmond_brakus , in category: PHP Frameworks , 5 months ago

How to connect mysql database using laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lottie , a month ago

@edmond_brakus 

To connect a MySQL database using Laravel, you need to follow these steps:

  1. Open your Laravel project in a code editor.
  2. Configure the database connection details in the .env file. Open the .env file and update the following lines with your MySQL database connection details:
1
2
3
4
5
6
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_mysql_username
DB_PASSWORD=your_mysql_password


  1. Next, open the config/database.php file and update the MySQL database configuration like this:
1
2
3
4
5
6
7
8
'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'your_database_name'),
    'username' => env('DB_USERNAME', 'your_mysql_username'),
    'password' => env('DB_PASSWORD', 'your_mysql_password'),
],


  1. Create a migration to create a table in the database. Run the following command in the terminal:
1
php artisan make:migration create_table_name


  1. Run the migration to create the table in the database:
1
php artisan migrate


  1. Now you can use the Laravel Eloquent ORM to interact with the MySQL database. You can create models, controllers, and views to perform CRUD operations on the MySQL database.


That's it! You have successfully connected a MySQL database using Laravel.