Blues Clues
Blues Clues

Reputation: 1848

Laravel sessions with sweet alert Session Loops

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?

Upvotes: 3

Views: 2697

Answers (1)

Dhaval Chheda
Dhaval Chheda

Reputation: 5177

a flash message has to be trigerred otherwise it will not make sense as you will set it everytime for the view

however you can use this code wherever the trigger is

Please Note :- I am just trigerring it everytime

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');
});

Hope this helps

Upvotes: 1

Related Questions