Laravel:检查会话中是否有可用值 [英] Laravel : check if value is available on session or not

查看:84
本文介绍了Laravel:检查会话中是否有可用值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是很累找出我该如何从会话中搜索值.我只是将一个ID存储到会话中.如果请求了相同的ID,则需要将其从会话中删除,如果没有可用的ID将其添加到会话中.

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.

这是我在会话中添加id的简单函数.

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'));
}

响应["1", "2", "3", "4", "1", "2"]

如何检查请求的ID是否已存储在会话中.

How can i check that requested id is already stored in session.

预期结果

public function addInquiry(Request $request) {
if $request->activity_id is already in session 
   { 
     // remove id from session 
   } else {
     // store in session 
   }
}
return response

我该怎么做?

推荐答案

使用 in_array() :

if (in_array($request->activity_id, session('cart'))) {

这篇关于Laravel:检查会话中是否有可用值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆