-2

I have a problem with showing error messages or session messages in laravel.

Comment the bootstrap cdn, but makes no differences.

have code like this, works correctly, refreshing the view, but not displaying errors:

Controller.php

return redirect()->route('trainers.show',[$trainer])->with('status','Entrenador actualizado correctamente');

blade.php

@if (session('status'))
    <div class="alert alert-success">
        {{session('status')}}
    </div>
@endif

Controller.php

$validatedData = $request->validate([
            'name' => 'required|max: 10',
            'avatar' => 'required|image',
            'slug' => 'required',
            'description' => 'required',
        ]);

blade.php

@if ($errors->any())
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $message)
        <li>{{ $message }}</li>
        @endforeach
    </ul>
</div>
@endif
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
jereconjota
  • 17
  • 1
  • 6
  • 3
    I think this is a duplicated problem. Please refer to: https://stackoverflow.com/questions/19838978/laravel-redirect-back-with-message – glinda93 Oct 07 '19 at 19:55
  • if you refresh the page, the flashed session vars are gone at that point, they are only available on the next request from when they are flashed ... unless you mean something else by refreshing the view – lagbox Oct 07 '19 at 21:31

1 Answers1

0

Regarding the session status, you can use \Session::has('status') and \Session::get('status'), like so:

@if(\Session::has('status'))
    <div class="alert alert-success">
        {{ \Session::get('status') }}
    </div>
@endif

And on your errors, you can also use has(), like so:

@if ($errors->has())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error}}</li>
            @endforeach
        </ul>
    </div>
@endif
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Lucas Arbex
  • 889
  • 1
  • 7
  • 15