Home > Web Development > Laravel Tutorials > Complete guide to using SQLite with Laravel 11

Complete guide to using SQLite with Laravel 11

Diego Cortés
Diego Cortés
January 21, 2025
Complete guide to using SQLite with Laravel 11

SQLite is an effective and lightweight tool for managing databases, and its integration with Laravel, the popular PHP framework, is a straightforward process. This detailed guide will teach you how to set up and use SQLite in your Laravel 11 project, allowing you to get the most out of both technologies. Below, you will find the necessary steps to get started.

What is SQLite?

SQLite is a C library that implements a serverless, self-contained, high-performance SQL database engine. It is ideal for smaller-scale applications where a complex server setup is not required. Its portability and simplicity make it a popular choice among developers and startups.

Installing Laravel 11

If you have not installed Laravel 11 yet, you first need to have Composer installed on your machine. Then, you can create a new Laravel project by running the following command in your terminal:

composer create-project --prefer-dist laravel/laravel project-name

Replace project-name with your desired project folder name.

Setting Up SQLite in Laravel

Step 1: Create a database file

Before configuring SQLite, you need to create a file for the database. Navigate to your project directory and run the following command:

touch database/database.sqlite

This command will create a file named database.sqlite in the database folder of your project.

Step 2: Configure the .env file

Open the .env file in the root of your project and locate the database settings. Change the following lines to configure the use of SQLite:

DB_CONNECTION=sqlite
DB_DATABASE=/path/to/your/project/database/database.sqlite

Make sure to modify /path/to/your/project with the corresponding path to your Laravel project.

Step 3: Configure the config/database.php file

Navigate to the config/database.php file and ensure that the configuration for SQLite is properly defined. Typically, the default settings should be sufficient, but it's good to double-check. This file includes an array of connections where sqlite should be defined as follows:

'sqlite' => [
    'driver' => 'sqlite',
    'url' => env('DATABASE_URL'),
    'database' => env('DB_DATABASE'),
    'prefix' => '',
],

Migrations with SQLite

Now that you have set up SQLite as your database, you can perform migrations. Migrations are an easy way to build and modify the database structure through code. To create a new migration, run the following command:

php artisan make:migration create_new_table

Then, you can define the table structure in the file that will be created in the database/migrations folder. When you're ready, apply the migration by executing:

php artisan migrate

Using SQLite in Applications

The use of SQLite can be seen in situations where the application requires a simple and lightweight database. Ideal for development and testing, SQLite allows developers to work efficiently without the need for a complex database setup. Furthermore, its single file makes data handling and transfer easy.

Conclusion

Integrating SQLite into your Laravel 11 project is a straightforward and effective process. By following these steps, you will be able to take advantage of all the functionalities of SQLite for your development needs. For more news and useful guides on technology and web development, I invite you to explore more content on my blog.

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 22.27 ms