Laravel custom errors

Last Updated: August 6, 2021

How to show custom errors in your Laravel Application

When you develop Laravel application you may need to send custom error messages from Laravel Controller to the Laravel View.

So you can have the following code inside the controller

return Redirect::back()->withErrors(['msg'=>'The Message']);

Here I am redirect to the same route
But if you want to redirect to a view you can use the following code

return view('your-view')->withErrors(['msg'=>'The Message']);

Now inside your view you can add the following code to show the error messages you received from the controller.

 @if($errors->any())
    <ul>
      @foreach ($errors->all() as $error)
       <li>{{ $error }}</li>
      @endforeach
    </ul>
 @endif