mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

Home Page:https://www.mongodb.com/compatibility/mongodb-laravel-integration

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature Request] SessionServiceProvider for database

masterbater opened this issue · comments

Is your feature request related to a problem?

Install laravel breeze react example, setup SESSION_DRIVER=database, the first error is duplicate key for id, deleting unique index solve this but in any post request, it will always cause 419 | PAGE EXPIRED

Temp solution just using any driver like file. I would like guidance on how to make Session driver database work for mongodb thanks

The package jenssegers/laravel-mongodb-session could be updated and merged into this package. Tracking this feature request in PHPORM-201

The package jenssegers/laravel-mongodb-session could be updated and merged into this package. Tracking this feature request in PHPORM-201

Thanks @GromNaN for tracking this hope you have more energy to maintain this library hehe.
🙇🏻‍♂️🙏🏻

You should be able to use Laravel's database storage with a MongoDB connection using. But that doesn't work 😞 because we overload Query\Builder::find($id) to look for _id field instead of id used by the DatabaseSessionHandler. I'll try to make the "id" field configurable in Laravel. Otherwise we will have to populate _id with the value of id (which would be better). May be related to #2489

Using the database driver and connection:

SESSION_DRIVER=database
SESSION_CONNECTION=mongodb

Also, you can use Symfony's MongoDbSessionHandler by following Laravel's documentation. This is a very optimized session handler, but I'm not sure every Laravel features are supported since it doesn't store this fields: user_id, ip_address, user_agent, and last_activity.

namespace App\Providers;

use Illuminate\Support\Facades\Session;
use Illuminate\Support\ServiceProvider;
use MongoDB\Laravel\Connection;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Session::extend('mongodb', function ($app) {
            /** @var Connection $connection */
            $connection = $app->get('db')->connection('mongodb');

            return new MongoDbSessionHandler(
                $connection->getMongoClient(),
                [
                    'database' => $connection->getDatabaseName(),
                    'collection' => 'sessions',
                ],
            );
        });
    }
}

And choose the driver SESSION_DRIVER=mongodb

commented

You should be able to use Laravel's database storage with a MongoDB connection using. But that doesn't work 😞 because we overload Query\Builder::find($id) to look for _id field instead of id used by the DatabaseSessionHandler. I'll try to make the "id" field configurable in Laravel. Otherwise we will have to populate _id with the value of id (which would be better). May be related to #2489

Using the database driver and connection:


SESSION_DRIVER=database

SESSION_CONNECTION=mongodb


Also, you can use Symfony's MongoDbSessionHandler by following Laravel's documentation. This is a very optimized session handler, but I'm not sure every Laravel features are supported since it doesn't store this fields: user_id, ip_address, user_agent, and last_activity.

namespace App\Providers;



use Illuminate\Support\Facades\Session;

use Illuminate\Support\ServiceProvider;

use MongoDB\Laravel\Connection;

use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandler;



class AppServiceProvider extends ServiceProvider

{

    public function boot(): void

    {

        Session::extend('mongodb', function ($app) {

            /** @var Connection $connection */

            $connection = $app->get('db')->connection('mongodb');



            return new MongoDbSessionHandler(

                $connection->getMongoClient(),

                [

                    'database' => $connection->getDatabaseName(),

                    'collection' => 'sessions',

                ],

            );

        });

    }

}

And choose the driver SESSION_DRIVER=mongodb

As always thanks @GromNaN, you even give me suggestions.

commented

Will this fix in minor version or in v5?
#3041

It'll be fixed it in a major version, which we hope to release at the end of August.

commented

It'll be fixed it in a major version, which we hope to release at the end of August.

I know you dont have beta release, Is there anyway I could test the v5 before release?

You can try my branch by using my repository on your composer.json

{
    "repositories": [
        { "type": "vcs", "url": "https://github.com/GromNaN/laravel-mongodb-fork" }
    ],
    "require": {
        "mongodb/laravel-mongodb": "dev-_id"
    }

Ultimately, the work will be done on the branch 5.0 of this repository, with more granular PRs.

@GromNaN gentle ping, is the v5 still be release at the end of august? id, automated date casting and stdClass in query builder seems could be a great starter

@GromNaN gentle ping, is the v5 still be release at the end of august? id, automated date casting and stdClass in query builder seems could be a great starter

It would be great if we can get an estimated date for the release!

The next major release (v5) is still in development. It should hopefully land within next couple of months.
Please follow PHPORM-212 to monitor the progress closely.
We appreciate your patience and enthusiasm!

commented

The next major release (v5) is still in development. It should hopefully land within next couple of months. Please follow PHPORM-212 to monitor the progress closely. We appreciate your patience and enthusiasm!

@GromNaN is already cooking and ready to serve soon. I would appreciate if the query builder stdclass and id alias and date conversion would be released in v5 and full laravel behavior in v6 (LTS even number)

full laravel behavior in v6 (LTS even number)

What do expect for v6 that you haven't listed for v5?