To enable Write-Ahead Logging (WAL) in SQLite for your Laravel application, you can indeed use the after parameter in your database.php configuration file. This is a common approach and is well-documented in various resources. However, you can also set the journal_mode directly in the SQLite connection configuration.
Here’s how you can do both:
Using the after Parameter
You can add an after parameter to your SQLite connection configuration in config/database.php to execute a PRAGMA statement after the connection is established. This is a flexible way to enable WAL mode.
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
'after' => function ($connection) {
$connection->statement('PRAGMA journal_mode=WAL;');
},
],
Using the journal_mode Parameter
Alternatively, you can directly set the journal_mode in the SQLite connection configuration. This method is less common but can be more straightforward.
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
'journal_mode' => 'WAL',
],
Documentation
The journal_mode parameter is not explicitly documented in the Laravel documentation, but it is a valid SQLite PRAGMA setting. You can find more information about SQLite PRAGMA statements in the SQLite documentation.
Summary
Both methods are valid for enabling WAL mode in SQLite. The after parameter is more flexible and can be used for other PRAGMA settings as well, while the journal_mode parameter is more direct and specific to setting the journal mode.
Choose the method that best fits your needs and coding style. If you are new to Laravel, using the after parameter might be more beneficial as it allows you to execute multiple PRAGMA statements if needed.