简体   繁体   中英

Removing a particular index/item from session array in Laravel

I have a Controller that adds a name of a university class to the session:

    // Get all requests
    $data = $request->all();

    .. input validation here ..

    // Push the class to the session
    Session::push('class', $data['class']);

This generates a nice to use array like this:

["ECEC 301 Advanced Programming for Engineers Lab","ECEC 301 Advanced Programming for Engineers Lecture"]

I also have this code that checks for duplicates like this:

    // Ensures no duplicate entries in the session
    if(Session::has('class')) {
        foreach(Session::get('class') as $class) {
            if($data['class'] === $class) {
                return Response::json(array(
                        'success' => false,
                        'code' => 0,
                        'message' => $data['class'] . ' already in the cart'
                    )
                );
            }
        }
    }

My question is - how do I remove a particular class from the session?

This is what I have:

    // Remove the item [if exists]
    if(Session::has('class')) {
        foreach(Session::get('class') as $class) {
            if($data['class'] === $class) {
                Session::pull('class', $data['class']);
                return Response::json(array(
                        'success' => true,
                        'code' => 1,
                        'message' => $data['class'] . ' removed from cart'
                    )
                );
            }
        }
    }

However, this seems to remove everything from the cart instead of just ECEC 301 Advanced Programming for Engineers Lab , for example.

As suggested, I used unset and array_values from this thread :

    // Remove the item [if exists]
    if(Session::has('class')) {
        $classes = Session::get('class');
        foreach($classes as $index => $class) {
            if($data['class'] === $class) {
                unset($classes[$index]);
                $newClass = array_values($classes);
                Session::put('class', $newClass);
                return Response::json(array(
                        'success' => true,
                        'code' => 1,
                        'class' => $classes,
                        'message' => $data['class'] . ' removed from cart'
                    )
                );
            }
        }
    }

You are using the Session::pull in a wrong way. Session::pull(a,b) pulls a from the session and removes it from the session. If a is not found in the session, then b is returned as a default value. Note that b is optional.

In order to replace the class value, you can just use

Session::put('class', $data['class']);

This will overwrite the old class value in the session.

Edit

I would advise you to rename the session key class to classes , as this key may contain multiple classes.

I would also advise to work with key => value pairs in the array, instead of with only values and implicit keys. This will allow you to easily delete a class from the classes array and store the new classes array in the session using the put method. It will also make it a lot easier to work with the selected classes in the backend and in the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM