Last active
July 24, 2020 21:07
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!