php 从 Laravel Session 数组中删除项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21242401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete items from Laravel Session array
提问by Jakob Fuchs
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. Then I tried looping and unsetting, which isn't working either. Any help is appreciated.
但我不知道如何删除它们。我试过 Session::forget('event_date_display')[$index],但这会删除整个数组。然后我尝试循环和取消设置,这也不起作用。任何帮助表示赞赏。
回答by patricksayshi
When you call Session::forget('event_data_display')[$index], there is no point at which that $indexvariable 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.
当您调用Session::forget('event_data_display')[$index]时,该$index变量不会被传递到forget()方法中。所以 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 addingto 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')beforeit looks for the value stored at the $iindex.
当您调用 时Session::get('event_data_display')[$i],之所以起作用,是因为 PHPSession::get('event_data_display')在查找存储在$i索引处的值之前检索了数组值。
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()方法只能对传递给它的内容起作用。
回答by TheLastCodeBender
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
我使用以下代码解决它。只需将您要在数组中删除的值作为 an"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;
}
}
}
}
回答by Jishnu RS
Add
添加
session()->save();
after clearing the session. Then only the values in session get deleted.
清除会话后。然后只有会话中的值被删除。
回答by thestepafter
It appears to me that you are trying to remove a value from an arraystored in the sessions. To do this I would think that you want to do the standard array unset($array[$key]).
在我看来,您正试图从array存储在sessions. 为此,我认为您想要执行标准数组unset($array[$key])。
So it would be something like unset(Session::$array[$key]);I believe.
所以它会像unset(Session::$array[$key]);我相信的那样。
回答by Jayant Pandey
Just use below code to remove all session in laravel
只需使用下面的代码删除laravel中的所有会话
$request->session()->flush();
$request->session()->flush();

