Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

php - Delete items from Laravel Session array

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

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'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.

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...