Sunil Dabhi
Sunil Dabhi

Reputation: 99

Laravel 8 Redirect back with message not working in blade template

I am using Laravel v8.16.1 and PHP v7.3.1 when I am trying to get a message in the blade template.

@if(session('error'))
    <div class="alert alert-success alert-dismissible">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        <h5><i class="icon fas fa-ban"></i> Success!</h5>
        {{ session('success') }}
    </div>
@endif

Using the below code, it's working for me.

$req->session()->flash('error', 'Invalid Username or Password');
return view('admin.views.login');

However, it's not the right way, and when I am trying to redirect back using the below code, then it's not working

return redirect()->back()->with('error', 'Invalid Username or Password');

Please help what is missing or what is wrong?

Upvotes: 1

Views: 7179

Answers (1)

Nitin Sharma
Nitin Sharma

Reputation: 197

You can use to show errors

@if(count($errors) > 0 )
<div class="alert alert-danger alert-dismissible fade show" role="alert">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
  </button>
  <ul class="p-0 m-0" style="list-style: none;">
    @foreach($errors->all() as $error)
    <li>{{$error}}</li>
    @endforeach
  </ul>
</div>
@endif

And the redirect code would be

return redirect()->back()->withErrors(['errors' => 'There is some error in the details.']);

Upvotes: 2

Related Questions