Home / Snippets / How to Redirect back with Input and Error Message in Laravel 8
How to Redirect back with Input and Error Message in Laravel 8 cover

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

3.9K

3 years ago

0 comments

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

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

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

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this