Skip to content

Connecting a database to Laravel

The Valley of Code

Your Web Development Manual

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
  • THE VALLEY OF CODE (+ PRO), your web development manual
  • I wrote 15+ free coding BOOKS, download them here
  • SOLOPRENEUR LAND the missing MBA for wannabe solopreneurs craving a life with more freedom, control, fulfillment and purpose (summer 2024)