Skip to content

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.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my Laravel Handbook

Here is how can I help you: