Laravel sessions with sweet alert Session Loops

时间:2017-07-12 07:59:51

标签: php session laravel-5 sweetalert

I'm just confused about my code. But I really thought that my code is correct. I'm trying to use with() method in Laravel 5.1 and then return to a view, then the sweet alert appears if the session that has been set is exists. Please see my code below:

PageController.php

return redirect()->route('list.view')->with('sweetalert', 'List has been created!');

view.blade.php

@extends('layout.master')

@section('container')
  @foreach($lists as $list)
    <li>{{ $list->name }}</li>
  @endforeach
@stop

master.blade.php

<div class="container">
  // some markup here...
</div>

@if(Session::has('sweetalert'))
<script>
    swal('Success!', '{{ Session::get('sweetalert') }}', 'success');
</script>
@endif

I only want it to appear once, but if I try to click the back button, the message appears again. I have also tried the ff. code but nothings change:

@if(Session::has('sweet'))
  <script>
      swal('Success!', '{{ Session::get('sweetalert') }}', 'success');
  </script>
  <?php Session::forget('sweetalert'); ?>
@endif

Little help here?

1 个答案:

答案 0 :(得分:0)

flash消息必须被触发,否则它将没有意义,因为你将每次为视图设置它

然而,只要触发器

,您就可以使用此代码

请注意: - 我每次只是在试图它?

Route::get('/', function () {
    session()->flash('testing', 'I see this'); // Please have this line inside the trigger so the session does not get created everytime the view is called
    return view('welcome');
});

希望这有帮助