Search code examples
laravellaravel-session

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


Solution

  • 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();