1ff / laravel-mongodb-session
A mongodb session driver for laravel
Installs: 32 515
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 7
Forks: 9
Open Issues: 2
pkg:composer/1ff/laravel-mongodb-session
Requires
- php: ^8.2
 - ext-mongodb: *
 - illuminate/session: ^12.0
 - mongodb/laravel-mongodb: ^5.0
 
Requires (Dev)
- orchestra/testbench: ^10.0
 - phpunit/phpunit: ^11.0
 
This package is auto-updated.
Last update: 2025-08-05 10:12:38 UTC
README
A MongoDB session driver for Laravel
| Laravel Version  | 
Package Version  | 
Install using this command  | 
|---|---|---|
| 12.x | 7.x.x | composer require 1ff/laravel-mongodb-session:^7.0 | 
| 11.x | 6.x.x | composer require 1ff/laravel-mongodb-session:^6.0 | 
| 10.x | 5.x.x | composer require 1ff/laravel-mongodb-session:^5.0 | 
| 9.x | 4.x.x | composer require 1ff/laravel-mongodb-session:^4.0 | 
| 8.x | 3.x.x | composer require 1ff/laravel-mongodb-session:^3.0 | 
| 7.x | 2.x.x | composer require 1ff/laravel-mongodb-session:^2.0 | 
| 5.x.x, 6.x | 1.x.x | composer require 1ff/laravel-mongodb-session:^1.0 | 
Installation
Install using composer:
composer require 1ff/laravel-mongodb-session
Change the connection in config/session.php to the name of the mongo connection from your config/database.php config
'connection' =>  'mongodb',
Update your .env file and change the SESSION_DRIVER to mongodb
SESSION_DRIVER=mongodb
Advantages
- This driver uses the MongoDB TTL indexes meaning when a session key expires it will be automatically deleted. So no need for garbage collection implementation.
 - This way, the collection's size will remain around the size you expect and won't get falsely filled with unused data.
 - The package automatically adds a migration which creates the index. If you change the name of the 
session.tableyou should rerun the ttl index creation commandphp artisan mongodb:session:index. 
Enjoy!
Testing
This package includes a comprehensive test suite to ensure the MongoDB session handler works correctly. The tests cover:
- Basic session operations (read, write, destroy)
 - Integration with Laravel's session system
 - HTTP session functionality
 - Laravel's testing helpers integration
 
Running the Tests
To run the tests, follow these steps:
- Make sure MongoDB is installed and running on your system
 - Install the package dependencies with Composer:
 
composer install
- Run the tests with PHPUnit:
 
vendor/bin/phpunit
Continuous Integration
The package includes a GitHub Actions workflow that automatically runs tests on PHP 8.1 with Laravel 10.x against MongoDB 7. The workflow:
- Sets up a MongoDB service container
 - Installs PHP with MongoDB extension
 - Caches Composer dependencies for faster builds
 - Runs the test suite
 
This ensures all tests pass before merging new changes.
Expected Test Results
When all tests are passing, you should see output similar to:
PHPUnit 10.x.x by Sebastian Bergmann and contributors.
...............                                                   15 / 15 (100%)
Time: 00:00.444, Memory: 32.00 MB
OK (15 tests, 41 assertions)
Testing Environments
The tests are compatible with:
- PHP 8.1+
 - Laravel 10.x
 - MongoDB 4.0+
 
Test Coverage
- Unit Tests: These test the 
MongoDbSessionHandlermethods directly (open, close, read, write, destroy, gc) - Feature Tests: These test the integration with Laravel's session functionality
 - HTTP Tests: These test session handling in HTTP requests and session persistence
 - Laravel Helper Tests: These test integration with Laravel's testing helpers like 
withSessionandflushSession 
If you encounter any issues with the tests, please submit an issue on the GitHub repository.