Skip to content

Instantly share code, notes, and snippets.

@gphg
Last active July 24, 2020 21:07
Show Gist options
  • Save gphg/12225c631afe934cbbc7c81f4a9f43a4 to your computer and use it in GitHub Desktop.
Save gphg/12225c631afe934cbbc7c81f4a9f43a4 to your computer and use it in GitHub Desktop.
Laravel/Lumen: load SQLite database relative path dynamically. Basically it check if database path IS NOT absolute, then reassign it with relative one.
<?php // bootstrap/app.php
// <=========== AFTER DOTENV LOEADED ================>
// Laravel/Lumen SQLite relative path
if ((getenv('DB_CONNECTION') === 'sqlite') // Check if BD driver is SQLite
&& (realpath(getenv('DB_DATABASE')) === false) // And database path IS NOT absolute
) { // Assign it to absolute with realpath() instead.
putenv("DB_DATABASE=".realpath(__DIR__.'/../' . getenv('DB_DATABASE')));
};
// <========= BEFORE APPLICATION DEFINED ============>
# This is the example .env file
APP_ENV=local
APP_DEBUG=true
APP_KEY=
APP_TIMEZONE=UTC
DB_CONNECTION=sqlite
DB_DATABASE=storage/laravel.sqlite3
CACHE_DRIVER=file
QUEUE_DRIVER=sync
@gphg
Copy link
Author

gphg commented Jul 24, 2020

Tested on Laravel/Lumen 5.6. I might want to put README.md or just this comment section is fine too.

If you find this useful, you can help other by post the Laravel/Lumen version you tested/using on this comment section.

Feedback are welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment