aref razavi
aref razavi

Reputation: 422

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

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

Upvotes: 2

Views: 391

Answers (2)

aref razavi
aref razavi

Reputation: 422

Yes I found it. the problem is to displacement of serialize() and base64_encode() after unset($payload['cart']) like this:

use App\Models\Session;

$session = Session::where('user_id', $request->user_id)->first();
$payload = unserialize(base64_decode($session->payload));
if (!isset($payload['cart'])){
    unset($payload['cart']);
}
$session->payload = base64_encode(serialize($payload));
$session->save();

Upvotes: 0

Lk77
Lk77

Reputation: 2462

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

Upvotes: 1

Related Questions