Connecting a database to Laravel
New Course Coming Soon:
Get Really Good at Git
We’re using Laravel in a very basic form, without any database.
Now I want to set up a database and configure Laravel to use it.
After we’ve configured the database, I’ll show you how to use forms to accept user input and store data in the database, and how to visualize this data.
I’ll also show you how you can use data from the database with dynamic routes.
Connect the database to Laravel
The easiest way to use a database is by using SQLite.
SQLite is just a file hosted in your site, no special setup needed.
Open the .env
file, and instead of the default configuration
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
add
DB_CONNECTION=sqlite
Laravel will automatically create a SQLite database in database/database.sqlite
the first time you run a migration.
→ Get my Laravel Handbook
Here is how can I help you:
- COURSES where I teach everything I know
- CODING BOOTCAMP cohort course - next edition in 2025
- BOOKS 16 coding ebooks you can download for free on JS Python C PHP and lots more
- Follow me on X