Create, Configure and Connect Laravel 10 with MySQL database with .env file
- Andrés Cruz

Как создать базу данных в Laragon


Подключение базы данных в проекте laravel
'mysql' => [
'driver' => 'mysql' ,
'url' => env( 'DATABASE_URL' ),
'host' => env( 'DB_HOST' , '127.0.0.1' ),
'порт' => env( 'DB_PORT' , '3306' ),
'база данных' => env( 'DB_DATABASE' , 'forge' ),
'имя пользователя' => env( 'DB_USERNAME' , 'forge' ),
'пароль' => env( 'DB_PASSWORD' , " ),
'unix_socket' => env( 'DB_SOCKET' , " ),
'charset' => 'utf8mb4' ,
'параметры сортировки' => 'utf8mb4_unicode_ci' ,
'префикс' => " ,
'prefix_indexes' => true ,
'strict' => true ,
'engine' => NULL,
'параметры' => extension_loaded( 'pdo_mysql' ) ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env( 'MYSQL_ATTR_SSL_CA' ),
]) : [],
],
The env file and function
The env file is the one that we have to use when we are in the development environment, therefore the configurations that we add in it are configurations in this environment.
On the other hand, when we go to the production environment, we must do without the .env file and Laravel automatically uses all the configurations that are inside the config folder.
The env function is simply a helper function provided by Laravel and allows us to search for the configuration established in the .env file given the key or its key as a parameter.
Connection to the database in Laravel in development environment (.env file)
So feel free to place the configurations in the database.php file but in my case, as we are developing the app, we are going to do it from the .env which, as I indicated, is the one we should use when developing; Finally, it would be as follows:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=larablog
DB_USERNAME=root
DB_PASSWORD=
And this would be all, of course, you have to go to your MySQL and through the graphical interface or from the console create the database that you are specifying in the project configurations.
I agree to receive announcements of interest about this Blog.
Connect a Laravel project to a MySQL database (for example); although we can connect to other types of databases such as SQLServer, PostgreSQL, etc.
- Andrés Cruz
This material is part of my complete course and book; You can purchase them from the books and/or courses section, Curso y Libro Laravel 12 con Tailwind Vue 3, introducción a Jetstream Livewire e Inerta desde cero - 2025.