Posts Learn Components Snippets Categories Tags About
/

How to Redirect back with Input and Error Message in Laravel 8

Learn how to redirect to the previous page after validation in Laravel with the input and error message to show validation to the user

Created on Aug 10, 2021

85 views

When making validation in Laravel you may need to redirect the user back to the previous page to show the error feedbacks. In order to do that, you will have to return the "back()" method and attaching the inputs and errors using "withInput()" and "withErrors()" from the controller that's responsible for the validation.

Full Code Example
The code example below should be placed within your controller method. 
<?php

# your controller class

public function store(Request $request)
{
    $providers = ['google', 'yahoo'];

    if (! $emailExists) {
        return back()
            ->withInput()
            ->withErrors([
                'email' => 'Your email must be from either one of these providers ' . collect($providers)->join(', ', ' or ')
            ]);
    }
}

Snippets by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your project.

View Components

Other Code Snippets

More