Here is the problem I have a session
session('products')
this is actually an array that contains id
session('products')
array:4 [▼
0 => "1"
1 => "2"
2 => "4"
3 => "1"
]
Now I want to delete lets say 4
How do I do that? I tried method
session()->pull($product, 'products');
But it didn't work!
Other solution
session()->forget('products', $product);
it also didn't work
You AFAIR have to firstly retrieve whole array, edit it and then set it again. If you want to delete by product ID, which is as I assume an array value, you can use this: PHP array delete by value (not key)
Misunderstood question
Session::pull
takes first parameter as the item do delete and second as the default value to return. You have mistaken the order of arguments. Try:As I can see in source,
Session::forget
expects string or array, so you should specify only the first parameter:This method isn't tested:
note the dot notation here, its worth the try. If that isnt working, you can always do this: