Home > OS >  how to session()->forget('cart') for another user in laravel?
how to session()->forget('cart') for another user in laravel?

Time:08-22

my session driver is set to database,

.env => SESSION_DRIVER=database

I have made a model for session and I access to a session of another user by user_id like this :

use App\Models\Session;

$payload = Session::where('user_id', $request->user_id)->pluck('payload');
$payload = unserialize(base64_decode($payload));
if (!isset($payload['cart'])) {
    dd($payload['cart']);
}

now I want to session()->forget('cart') of that specific user not the current user, but the payload field is decode by base64 and serialized.

how to do that?

thanks

CodePudding user response:

Since you can find the session id then there should be a way to load the given session. Maybe try something like:

$id = Session::where('user_id', $request->user_id)->value('id'); // value gets the first id
$sessionDriver = Session::driver();
$store = new \Illuminate\Session\Store(
    $this->config->get('session.cookie'),
    $sessionDriver,
    $id
);
$store->start();
$store->pull('cart');
$store->save();

Note: I have not tested this but I think it may work

CodePudding user response:

I tried a few things and by changing the id it works :

// Get the store from laravel (encrypted or not)
$store = session()->getDrivers()['database'];
// Change the id
$store->setId($id);
// Start the session
$store->start();
// Remove the item
$store->pull('cart');
// Save the session in database
$store->save();

i don't think it's something that laravel support, so this might break in the future

  • Related