I have problems understanding how sessions work in laravel and I'm getting issues trying to do what I need for my project.
I want to store an array of IDs on a session variable, nothing more than that, and that array needs to be updated if the user selects one or more items that aren't already in that session array. I've tried two approaches to this: first checking if the item exists looping through the session variable with foreach (that gave me problems, I had to assign the session to a variable to work) and if it's not there, just push the new id. All of this is giving me problems since I also need to delete the id's if the user unchecks them on the front. Sessions just don't seem to work like arrays, which is what I'm basically trying to use them as.
Also, I need to be able to add items to the array one by one or more than one at a time, and array_merge is not helping me here either
This works, I guess...
$key = 'UserChoices';
Session::put($key, [10,22,31]); /* Lets start with some data */
$choices = Session::get($key); /* Get all ids. returns (array) */
$idExists = in_array($choices, $id); /* Id exists ? returns (bool) */
$choices[] = $idToAdd; /* Add a single id */
$choices = array_merge($choices, $arrayOfIds); /* Ad an array of ids */
unset( $choices[$idToRemove] ); /* Remove an id */
Session::put($key, $choices); /* Just save it ! */
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community