Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

curtismchale's avatar

SQLite Write Ahead Logging

I'd like to enable WAL but am unsure how to do it in the best way.

This blog post shows adding it to my database.php connection information using the after parameter. But I also see a specific entry called journal_mode in the database connection that I assume could be used for the same purpose and would look like 'journal_mode' => 'WAL' but I haven't been able to find the documentation on the available options for journal_mode.

I'm new to Laravel so if someone knows where the docs are for this option I'd love to know and not ask the question next time.

0 likes
2 replies
LaryAI's avatar
Level 58

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.

Please or to participate in this conversation.