Posts Learn Components Snippets Categories Tags Tools 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

3821 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 ')
            ]);
    }
}

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

)