2

I'm creating a cart system, this is my code to input some itens into the user Session:

public function jsonResponse($data){
    return response()->json([
        'success' => true,
        'users' => $data
    ]);
}

public function post(Request $request ,User $user)
{
    $request->session()->push('users', $user);
    return $this->jsonResponse($request->session()->get('users'));
}

How can I delete an unique item from the users array?


Alternative 01

It's able to remove the item from the users array with the following code:

public function delete(Request $request, User $user)
{
    $users = $request->session()->get('users');

    foreach ($users as $key => $val) {
        if($user->id == $users[$key]->id){
            $array = $request->session()->pull('users', []);
            unset($array[$key]);
            $request->session()->put('users', $array);
            return $this->jsonResponse($request->session()->get('users'));
        }
    }

    return $this->jsonResponse($request->session()->get('users'));
}

But I was searching for a clean way... Without remove the array and put it back to the Session...


Solution 01

The following alternative has been found for a cleaner code:

public function delete(Request $request, User $user)
{
    $users = $request->session()->get('users');

    foreach ($users as $key => $val) {
        if($user->id == $users[$key]->id){
            $request->session()->forget('users.'.$key);
            return $this->jsonResponse($request->session()->get('users'));
        }
    }

    return $this->jsonResponse($request->session()->get('users'));
}

Thanks to Kyslik for remind the dot notation...

1 Answer 1

1

You can use forget() or pull() methods for that.

$request->session()->forget('key');

The forget method will remove a piece of data from the session

$request->session()->pull('key', 'default');

The pull method will retrieve and delete an item from the session in a single statement

Sign up to request clarification or add additional context in comments.

5 Comments

But my users key is an array and I need to delete just a specific item from the array, not the entire array, with the forget() method I was just able to remove the entire array, or am I doing it wrong?
Then pull() it, make changes (by using unset(), for example) and then push() it back.
I already do that, but I was thinkig if there is a clear way... Look at my edit...
@CaioKawasaki I am not sure and can not check at the moment, but try using dot notation, ->forget('array.key');
@Kyslik I had tried something like: $request->session()->forget('users'.$key), but when I read your comment I went to see the code and recognized that I had let a detail pass, it worked like this: $request->session()->forget('users.'.$key), nice =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.