Laravel

Laravel redirect back with error message example

In this article, we will learn about how we can redirect back with error messages as well with the old messages so that on the page refresh our input field will be auto-populated.

we almost use laravel default validate a method for validation because it will automatically redirect back to your previous form page. But sometimes you need to set a custom validation check or if condition then you can also redirect back() with an error message like as default error message work.

In the below example I have tried to show you the simplest example, with the custom error message.

Let’s see how we can do without using without input

return Redirect::back()->withErrors(['email.required' => 'Your Message']);

Example of showing error with input

return Redirect::back()
    ->withInput()
    ->withErrors(['email.required', 'Your Message']);

In the blade, file add the below code to see the error message

@if($errors->any())
   <h4>{{$errors->first()}}</h4>
@endif

I hope now you understood how you can override the error message by using return redirect back in laravel.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button