问题
Just tired to find out how can i search the value from session.I just stored an id to the session.After that if same id requested, need to remove it from session, if id not available add it to the session.
Here is my simple function that added id on session.
public function addInquiry(Request $request) {
Session::push('cart', $request->activity_id);
return response()->json(Session::get('cart'));
}
What am getting on response ["1", "2", "3", "4", "1", "2"]
How can i check that requested id is already stored in session.
expected result
public function addInquiry(Request $request) {
if $request->activity_id is already in session
{
// remove id from session
} else {
// store in session
}
}
return response
How can i do this ?
回答1:
Use in_array():
if (in_array($request->activity_id, session('cart'))) {
来源:https://stackoverflow.com/questions/48686040/laravel-check-if-value-is-available-on-session-or-not