Himanshu SharmaSoftware engineer
Let’s talk

Laravel · Lesson 15 of 40

Connecting Laravel to SQLite

See how Laravel picks a database from .env and config, inspect the default SQLite file with db:show and the query builder, and learn what to change for MySQL.

12 minDatabase and migrationsUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonExplain how the database connection is configured, inspect the SQLite database with Artisan, run parameterised queries with the DB facade, and know the .env keys for MySQL.

What you can do after this lesson

You will know where Pagelane Books' data lives, how Laravel finds it, and how to look inside without writing SQL. If you want a refresher on SQL itself, the SQL course and MySQL course cover it.

SQLite out of the box

Most web apps keep data in a relational database. Laravel supports several: SQLite, MySQL, MariaDB, PostgreSQL and SQL Server. A fresh Laravel 13 project is configured for SQLite, which stores everything in one ordinary file. The installer created database/database.sqlite and ran the first migrations (that is why the project already had tables). The relevant .env line:

text
DB_CONNECTION=sqlite

In config/database.php, 'default' => env('DB_CONNECTION', ...) picks which entry of the connections array is used, and the sqlite entry points at database/database.sqlite. There is nothing to start and no password. The .env.example file lists commented-out keys for other servers (DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD). The Laravel installation guide shows how to switch: set DB_CONNECTION=mysql and fill in those keys, create the database yourself, then run php artisan migrate.

text
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=pagelane
DB_USERNAME=shop_user
DB_PASSWORD=change-me-fake-value

Those are made-up values for illustration. Real passwords go only in the real .env and never in a lesson, a chat or Git. This course keeps SQLite so that everything you read can be repeated by running one command.

Looking inside the database

Artisan can describe the database:

bash
php artisan db:show
text
  SQLite .............................................................. 3.53.4
  Connection .......................................................... sqlite
  Database .......................................... database/database.sqlite
  Tables ................................................................... 9

  Schema / Table ........................................................ Size
  main / cache
  main / cache_locks
  main / failed_jobs
  main / job_batches
  main / jobs
  main / migrations
  main / password_reset_tokens
  main / sessions
  main / users

(Trimmed output captured on 2026-11 with SQLite 3.53.4; yours will differ.) Those nine tables come from three migrations. sessions and password_reset_tokens are created by the users migration. To see the columns of one table:

bash
php artisan db:table users
text
  Column ................................................................ Type
  id integer, autoincrement .......................................... integer
  name varchar ....................................................... varchar
  email varchar ...................................................... varchar
  email_verified_at datetime, nullable .............................. datetime
  password varchar ................................................... varchar
  remember_token varchar, nullable ................................... varchar
  created_at datetime, nullable ..................................... datetime
  updated_at datetime, nullable ..................................... datetime

  Index ......................................................................
  primary id ......................................................... primary
  users_email_unique email ............................................ unique

And which migrations have run:

bash
php artisan migrate:status
text
  0001_01_01_000000_create_users_table ............................... [1] Ran
  0001_01_01_000001_create_cache_table ............................... [1] Ran
  0001_01_01_000002_create_jobs_table ................................ [1] Ran

If you have the sqlite3 command-line tool, you can also open the file directly with sqlite3 database/database.sqlite ".tables", or run php artisan db to open the right client for the configured driver. Do not edit database.sqlite by hand while the app is writing to it.

Running queries from PHP

The DB facade runs SQL. For anything that includes a value, use placeholders and pass a binding array, so the driver sends the value separately from the SQL. Concatenating a value into the SQL string is how SQL injection happens.

php
use Illuminate\Support\Facades\DB;

DB::select('select ? as one, ? as two', [1, 2]);

DB::table('migrations')->count();
DB::table('migrations')->orderBy('id')->limit(2)->pluck('migration')->all();
DB::table('migrations')->where('batch', 1)->select('id', 'migration')->first();

Run through php artisan tinker --execute=... these gave a row with one and two, the count 3, the first two migration names, and the first migration row. DB::table(...) is the query builder, a fluent way to write queries in PHP that also binds values for you. In a few lessons Eloquent will sit on top of it so you rarely write either by hand.

Which database should you choose

SQLite is excellent for learning, testing and small, low-write sites: no setup and trivial backups (copy the file). MySQL, MariaDB or PostgreSQL are better when many users write at the same time, when several servers share one database, or when your host provides one. The code in this course works the same either way because Eloquent and migrations abstract most differences. Test with the database you will deploy on.

Common mistakes

  • Committing database.sqlite. The skeleton's database/.gitignore already excludes it; keep it ignored.
  • Putting values straight into raw SQL strings. Always use bindings.
  • Forgetting that config may be cached. If .env changes seem ignored, run php artisan config:clear.
  • Running tinker against a production database by accident. Check php artisan env and about first.
  • Assuming SQLite and MySQL behave identically for every type and function. Check unusual features on your target database.

Exercise

Using tinker and the query builder, print the number of rows in users and the name of the newest migration. Then run db:table for the sessions table and note its columns.

Show one possible answer
bash
php artisan tinker --execute='use Illuminate\Support\Facades\DB; echo DB::table("users")->count(), PHP_EOL; echo DB::table("migrations")->orderByDesc("id")->value("migration"), PHP_EOL;'
php artisan db:table sessions

On a fresh project the first line prints 0 and the second 0001_01_01_000002_create_jobs_table. The sessions table has columns such as id, user_id, ip_address, user_agent, payload and last_activity. Now create your own tables with migrations.

Key points

  • A fresh Laravel 13 project uses SQLite: a database stored in the single file database/database.sqlite, with no server to install.
  • DB_CONNECTION in .env selects the connection defined in config/database.php; other drivers need host, port, database name, user and password keys.
  • php artisan db:show and db:table describe the database without any SQL, and migrate:status lists which migrations ran.
  • The DB facade runs raw queries with bound parameters and offers a fluent query builder; always bind values instead of concatenating them.
  • SQLite is fine for learning and small sites; choose MySQL or PostgreSQL when you need many concurrent writers or a shared database server.

Check yourself

1. Where is the default SQLite database stored in a fresh Laravel project?
2. How should you pass a user value into a raw query?
3. Which command lists the tables and their sizes without SQL?
4. Which .env key chooses the database driver?