Laravel : check if value is available on session or not

可紊 提交于 2019-12-24 05:11:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!