从 Laravel Session 数组中删除项目 [英] Delete items from Laravel Session array

查看:31
本文介绍了从 Laravel Session 数组中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Laravel 会话数组中删除项目,但到目前为止还没有成功.

im trying to delete items from the Laravel Session Array, but with no luck so far.

我使用 Session::push() 方法将我的值存储在 Session 数组中:

I store my values in a Session array using the Session::push() method:

  Session::push('event_date_display', Input::get('event_date'));
  Session::push('event_start_display', Input::get('event_start'));
  Session::push('event_entry_display', Input::get('event_entry'));
  Session::push('event_end_display', Input::get('event_end'));

我可以像普通数组一样访问值:

I can access the values like normal arrays:

        @for($i=0;$i<Session::get('dates');$i++)
          <tr>
            <td>{{ Session::get('event_date_display')[$i] }}</td>
            <td>{{ Session::get('event_start_display')[$i] }}</td>
            <td>{{ Session::get('event_entry_display')[$i] == '' ? '-' : Session::get('event_entry_display')[$i] }}</td>
            <td>{{ Session::get('event_end_display')[$i] == '' ? '-' : Session::get('event_end_display')[$i] }}</td>
            <td><a href="{{URL::route('termin-loeschen', $i)}}" class="del"><span class="icon-spam"></span>Loeschen {{ $i }}</a></td>
          </tr>
        @endfor

但是我不知道如何删除它们.我试过 Session::forget('event_date_display')[$index],但这会删除整个数组.然后我尝试循环和取消设置,这也不起作用.任何帮助表示赞赏.

But I can't figure out how to delete them. I tried Session::forget('event_date_display')[$index], but this deletes the whole array. Then I tried looping and unsetting, which isn't working either. Any help is appreciated.

推荐答案

当你调用 Session::forget('event_data_display')[$index] 时,$index 变量被传递到 forget() 方法中.所以 Laravel 不会看到它,并且会取消设置 Session 数组的整个 'event_data_display' 索引.

When you call Session::forget('event_data_display')[$index], there is no point at which that $index variable gets passed into the forget() method. So Laravel won't see it, and will unset the entire 'event_data_display' index of the Session array.

要取消设置该索引处的值,您可能需要执行以下操作:

To unset the value at that index, you'll probably need to do something like this:

$event_data_display = Session::get('event_date_display');
unset($event_data_display[$index]);
Session::set('event_data_display', $event_data_display);

Laravel 的 Session 确实支持通过这样的指定索引添加到数组:

Laravel's Session does support adding to arrays via a specified index like this:

Session::push('user.teams', 'developers');

所以你也可以像这样访问数组的索引:

So you might also be able to access that index of the array like so:

Session::forget('event_data_display.' . $i);

我还没有尝试过,但值得一试.

I haven't tried it but it's worth a shot.

当您调用 Session::get('event_data_display')[$i] 时,有效的原因是 PHP 从 Session::get('event_data_display') before 它查找存储在 $i 索引中的值.

When you call Session::get('event_data_display')[$i], the reason that works is because PHP retrieves the array value from Session::get('event_data_display') before it looks for the value stored at the $i index.

当您调用 Session::forget('event_data_display') 时,forget() 方法只能对传递给它的内容起作用.

When you call Session::forget('event_data_display'), the forget() method can only act on what is passed to it.

这篇关于从 Laravel Session 数组中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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