Search code examples
authenticationjwtlaravel-5.3registrationpostman

Call to undefined method Illuminate\Http\JsonResponse::validate()


I am implementing a registration form and sending the post requests using postman with Laravel 5.3

It is working fine for web but I see the following exception in postman

FatalErrorException in RegistersUsers.php line 31:
Call to undefined method Illuminate\Http\JsonResponse::validate()

My validator method in Auth\RegisterController

   protected function validator(array $data)
    {
        if(Request::wantsJson()) {
            $rules  =   array(
            'first_name'    =>  'required',
            'last_name'     =>  'required',
            'email'         =>  'required',
            'password'      =>  'required',
            'phone_number'  =>  'required',

            );
            $validator  =   Validator::make($data, $rules);
            if($validator->fails()) {
                return response()->json($validator->messages());

            }
        } else {
            return Validator::make($data, [
                'name' => 'required|max:255',
                'email' => 'required|email|max:255|unique:users',
                'password' => 'required|min:6|confirmed',
            ]);

        }

    }

Full stack trace of error is

FatalErrorException in RegistersUsers.php line 31:
Call to undefined method Illuminate\Http\JsonResponse::validate()
in RegistersUsers.php line 31
at FatalErrorException->__construct() in HandleExceptions.php line 133
at HandleExceptions->fatalExceptionFromError() in HandleExceptions.php line 118
at HandleExceptions->handleShutdown() in HandleExceptions.php line 0
at RegisterController->register() in Controller.php line 55
at call_user_func_array:{/var/www/iproximity-web/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:55}() in Controller.php line 55
at Controller->callAction() in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch() in Route.php line 190
at Route->runController() in Route.php line 144
at Route->run() in Router.php line 653
at Router->Illuminate\Routing\{closure}() in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}() in RedirectIfAuthenticated.php line 24
at RedirectIfAuthenticated->handle() in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}() in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}() in SubstituteBindings.php line 41
at SubstituteBindings->handle() in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}() in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}() in ThrottleRequests.php line 49
at ThrottleRequests->handle() in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}() in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}() in Pipeline.php line 104
at Pipeline->then() in Router.php line 655
at Router->runRouteWithinStack() in Router.php line 629
at Router->dispatchToRoute() in Router.php line 607
at Router->dispatch() in Kernel.php line 268
at Kernel->Illuminate\Foundation\Http\{closure}() in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}() in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle() in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}() in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}() in Pipeline.php line 104
at Pipeline->then() in Kernel.php line 150
at Kernel->sendRequestThroughRouter() in Kernel.php line 117
at Kernel->handle() in index.php line 54
at {main}() in index.php line 0

Solution

  • The validator method is supposed to return a Validator instance. In the case of Request::wantsJson() you return a JsonResponse (on failures) or nothing at all (on success). You should probably return the $validator instead.