Connecting a database to Laravel
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.
→ I wrote 17 books to help you become a better developer:
- C Handbook
- Command Line Handbook
- CSS Handbook
- Express Handbook
- Git Cheat Sheet
- Go Handbook
- HTML Handbook
- JS Handbook
- Laravel Handbook
- Next.js Handbook
- Node.js Handbook
- PHP Handbook
- Python Handbook
- React Handbook
- SQL Handbook
- Svelte Handbook
- Swift Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025