How to Use SQLite Database in Laravel
Laravel supports many database connections such as MySQL, PostgreSQL, SQL Server and SQLite. Most of the time we use either MySQL or PostgreSQL. But if we need, we can easily change the default database connection of a Laravel application to SQLite. SQLite is a file based database and it is suitable for small projects. To change the connection, first, open the .env
file and change the DB_CONNECTION
to sqlite
. Comment or completely remove other database configuration lines like this.
DB_CONNECTION=sqlite
#DB_HOST=127.0.0.1
#DB_PORT=3306
#DB_DATABASE=database
#DB_USERNAME=root
#DB_PASSWORD=
Then we need to create a new file called database.sqlite
in the database
directory by running the following command.
touch database/database.sqlite
Now check whether the SQLite database has been successfully detected or not by Laravel. For that, run the migrate:status
command.
php artisan migrate:status
If you get a message called “Migration table not found.”, that means Laravel has successfully detected the database.sqlite
file and you are ready to run the migration. For that, run this migrate
Artisan command.
php artisan migrate
If you get the “Database does not exist.” or similar error, try to follow the How To Fix Laravel SQLite “Database does not exist” to fix it. Otherwise, it should create all the necessary tables in the SQLite database.