[英]Delete items from Laravel Session array
im trying to delete items from the Laravel Session Array, but with no luck so far. 我试图从Laravel会话阵列中删除项目,但到目前为止没有运气。
I store my values in a Session array using the Session::push() method: 我使用Session :: push()方法将我的值存储在Session数组中:
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
But I can't figure out how to delete them. 但我无法弄清楚如何删除它们。 I tried Session::forget('event_date_display')[$index], but this deletes the whole array.
我尝试了Session :: forget('event_date_display')[$ index],但这会删除整个数组。 Then I tried looping and unsetting, which isn't working either.
然后我尝试循环和取消设置,这也不起作用。 Any help is appreciated.
任何帮助表示赞赏。
When you call Session::forget('event_data_display')[$index]
, there is no point at which that $index
variable gets passed into the forget()
method. 当你调用
Session::forget('event_data_display')[$index]
,没有必要将$index
变量传递给forget()
方法。 So Laravel won't see it, and will unset the entire 'event_data_display' index of the Session array. 所以Laravel不会看到它,并且会取消设置Session数组的整个'event_data_display'索引。
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's Session does support adding to arrays via a specified index like this: Laravel的Session支持通过指定的索引添加到数组,如下所示:
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. 我没试过,但值得一试。
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::get('event_data_display')[$i]
, Session::get('event_data_display')[$i]
的原因是因为PHP 在查找存储在$i
索引处的值之前从Session::get('event_data_display')
检索数组值。
When you call Session::forget('event_data_display')
, the forget()
method can only act on what is passed to it. 当您调用
Session::forget('event_data_display')
, forget()
方法只能对传递给它的内容起作用。
I solve it using the following code. 我使用以下代码解决它。 Just pass the value that you want to delete in the array as an
"id"
to your controller like the code bellow 只需将数组中要删除的值作为
"id"
传递给控制器,如下面的代码所示
public function removeAddTocart(Request $request)
{
$remove = ''.$request->id.'';
if (Session::has('productCart'))
{
foreach (Session::get('productCart') as $key => $value)
{
if ($value === $remove)
{
Session::pull('productCart.'.$key); // retrieving the value and remove it from the array
break;
}
}
}
}
Add 加
session()->save();
after clearing the session. 清除会话后。 Then only the values in session get deleted.
然后只删除会话中的值。
It appears to me that you are trying to remove a value from an array
stored in the sessions
. 在我看来,您正在尝试从存储在
sessions
的array
删除值。 To do this I would think that you want to do the standard array unset($array[$key])
. 要做到这一点,我认为你想要标准数组
unset($array[$key])
。
So it would be something like unset(Session::$array[$key]);
所以它会像
unset(Session::$array[$key]);
I believe. 我相信。
Just use below code to remove all session in laravel 只需使用下面的代码删除laravel中的所有会话
$request->session()->flush(); $请求 - >会话() - >刷新();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.