Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nafeeur10's avatar

How to show Custom Request errors in API

Hello, I am using Laravel 9 for building API.

In registration, I have created a request file named 'RegisterRequest.' In that, I have written some rules. Like the below:

class RegisterRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        return [
            'name'      => 'required|min:3|max:50',
            'email'     => 'required|email:rfc,dns|unique:users',
            'password'  => 'required|confirmed|min:6'
        ];
    }
}

In my AuthController, how will I get that error validation if it fails? In postman, it's redirecting to the home route. I want to show them in the front end.

public function register(RegisterRequest $request)
{
        if ($request->fails()) {
            return "Error";
        }
        dd($request->validated());
}
0 likes
4 replies
nafeeur10's avatar

@Sinnbeck Removed that and given application/json. Now it's always going to root directory.

Please or to participate in this conversation.