0

In my controller:

 Session::flash('created', "Student created");
 return Redirect::to('student');

In my view:

 @if(Session::has('created'))
 alert('updated');
 @endif
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
Milan Suthar
  • 352
  • 1
  • 2
  • 17

3 Answers3

1

For me, instead of using Session::get('key'), I use Session::pull('key') and it only popup a single time.

E.g

@if(Session::has('success'))
    {{ Session::pull('success') }} 
@endif  

According to Laravel 7.x doc

The pull method will retrieve and delete an item from the session in a single statement:

ronaldtgi
  • 687
  • 8
  • 17
0

Display session message

@if(Session::has('success'))
  <div class="alert alert-success alert-dismissable alert-box">
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    {{ Session::get('success') }}               
  </div>
@endif
@if(Session::has('error'))
  <div class="alert alert-danger alert-dismissable alert-box">
    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    {{ Session::get('error') }} 
  </div>
@endif  

Called from controller

return redirect()->back()->with('success', 'Data added successfully');
Komal
  • 2,716
  • 3
  • 24
  • 32
0

Please avoid the unnecessary use of session. You can use Redirect::route. Check below example:

1) Initialize $strChecked = false; into your student route controller and then set $strChecked = true; into you student update method.

2) Replace your return line with return \Redirect::route('student')->with('strChecked', $strChecked);

3) Then in your view, you can use:

@if($strChecked)
    alert('updated');
@endif

EDITS:

Let me illustrate with my working demo code:

1) ROUTE:

Route::get('flash', 'FlashController@flashIndex')->name('frontend.flash');
Route::post('flash/update', 'FlashController@studentUpdate')->name('frontend.flash.update');

2) VIEW:

@section('content')
{{ Form::open(['route' => 'frontend.flash.update', 'class' => 'form-horizontal']) }}

{{ Form::submit('Demo', ['class' => 'btn btn-primary', 'style' => 'margin-right:15px']) }}  

{{ Form::close() }}

@endsection
@section('after-scripts-end')
    <script>
        @if(Session::has('created'))
         alert('updated');
        @endif
    </script>
@stop

3) CONTROLLER:

//Method to display action...
public function flashIndex()
{
 return view('frontend.flash');  
}

//Method to update action...
public function studentUpdate()
{
 Session::flash('created', "Student created");
 return Redirect::to('flash'); 
}

Hope this clears your doubts.

Above example working fine at my end. Describe your code if this is not work for you.

Community
  • 1
  • 1
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57