DEV Community

Devtonight
Devtonight

Posted on • Updated on • Originally published at devtonight.com

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=
Enter fullscreen mode Exit fullscreen mode

Then we need to create a new file called database.sqlite in the database directory by running the following command.

touch database/database.sqlite
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If you get the “Database does not exist.” or similar error, try to follow the How To Fix Laravel SQLite "Database does not exist" question to fix it. Otherwise, it should create all the necessary tables in the SQLite database.

Top comments (1)

Collapse
 
6hislain profile image
Mugemana Ghislain📌
DB_CONNECTION=sqlite
#DB_HOST=127.0.0.1
#DB_PORT=null
#DB_DATABASE=null
#DB_USERNAME=null
#DB_PASSWORD=null
Enter fullscreen mode Exit fullscreen mode

make sure the other variables except DB_CONNECTION=sqlite are commented. If you're on linux make the database/ directory writable

chmod 777 database && chmod 777 database/database.sqlite
Enter fullscreen mode Exit fullscreen mode