A way to define database connection in Laravel, is to define database details in .env file inside your root laravel directory. .env.example file will always provided in fresh installation of Laravel, when we install laravel with composer, .env file will automatically copied from .env.example. If you work in a team, it strongly recommended to only push .env.example because each developer have different configuration.
here an example of .env.example file
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
DB_FTM_HOST=127.0.0.1
DB_FTM_PORT=3306
DB_FTM_DATABASE=homestead
DB_FTM_USERNAME=homestead
DB_FTM_PASSWORD=secret
BROADCAST_DRIVER=log
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
Editing .env File
If you don’t fine .env file, you may to copy .env.example file and rename it to .env. don’t directly rename .env.example file except you work alone.
In .env file, you will find
....
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
....
DB_CONNECTION tell Laravel what kind of database machine you use, DB_HOST is the address of the database, because your database is in your local machine, so it will always 127.0.0.1 which mean is localhost. DB_PORT is your database port, by default, mysql will always use 3306 as it default port. DB_DATABASE is the name of your database, DB_USERNAME is a username that you use to login to your database while DB_PASSWORD is your username password.
Here the example of use
....
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_database
DB_USERNAME=root
DB_PASSWORD=my_password
....