Setting up SQLite for Laravel

Written by SarahS

1st June 2020

Sometimes you may decide you want to use SQLite for your Laravel app instead of a full blown MySQL database. In that case setting it up is pretty simple. Following the instructions in the Laravel docs, first you need to set up a SQLite database by running this command in Terminal:

touch database/database.sqlite

Then you have to edit your .env file with the following details:

DB_CONNECTION=sqlite
DB_DATABASE=/absolute/path/to/database.sqlite

So that is really the only tricky bit. I remove all the DB settings in the .env file and replace them with the above. If you don’t know what your absolute path is then it will look something like this:

/Users/username/projects/projectname/database/database.sqlite

Alternatively, you the default in the config/database.php file is:

'sqlite' => [
    'driver' => 'sqlite',
    'url' => env('DATABASE_URL'),
    'database' => env('DB_DATABASE', database_path('database.sqlite')),
    'prefix' => '',
    'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],

So you can just remove all DB settings from .env and it should also work. DOn’t forget to change the database setting to be:

'default' => env('DB_CONNECTION', 'sqlite'),

And that should do it. You can now use SQLite in your app the same way you would MySQL.

This is one of my Laravel posts.  I’m still learning Laravel so the posts are usually pretty basic and are more aimed at keeping track of things that I find useful so that I can come back to them again at a later date.  I hope you find them useful too.  If not, or if you think they are wrong then please leave a comment below.  Thank you.

Faster Laravel Hosting

If this post helped you or if you have any questions please leave me a comment below:

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…