skip to Main Content

(im new with laravel)
postman api test error like this all the time

"status": false,
"message": "Method Illuminate\Validation\Validator::false does not exist."

the function for register a new user for test

<?php

namespace AppHttpControllersApi;

use AppModelsUser;
use IlluminateHttpRequest;
use IlluminateSupportCarbon;
use AppHttpControllersController;
use IlluminateSupportFacadesِAuth;
use IlluminateSupportFacadesHash;
use IlluminateSupportFacadesValidator;


class UserController extends Controller
{
    /**
     * Create User
     * @param Request $request
     * @return Response
     */

    public function createUser(Request $request)
    {
        try {

             $validateUser = Validator::make($request->all(),
            [      
                'name'=> 'required',
                'email' => 'required|email|unique:users,email',
                'password' => 'required'
            ]);

            if($validateUser->false()){
                return response()->json([
                    'status' => false,
                    'message' => 'validation error',
                    'errors' => $validateUser->errors()
                ], 401);
            }

          $user = User::ceate([
            'name' => $request->name,
            'message' => $request->email,
            'password' => $request->password
          ]);
           
            return response()->json([
                'status' => true,
                'message' => 'User Created Succssfully',
                'token' => $userInfo
            ], 200);

        } catch(Throwable $th){
            return response()->json([
                'status' => false,
                'message' => $th->getMessage()
            ], 401);
        }
    }

}

can any one help me i use (use Validator;) and still get same error !!

2

Answers


  1. Use $validateUser->fails() instead of $validateUser->false()

    Login or Signup to reply.
  2. I believe you are looking for fails, not false, from the Laravel documentation:

    <?php
     
    namespace AppHttpControllers;
     
    use AppHttpControllersController;
    use IlluminateHttpRedirectResponse;
    use IlluminateHttpRequest;
    use IlluminateSupportFacadesValidator;
     
    class PostController extends Controller
    {
        /**
         * Store a new blog post.
         */
        public function store(Request $request): RedirectResponse
        {
            $validator = Validator::make($request->all(), [
                'title' => 'required|unique:posts|max:255',
                'body' => 'required',
            ]);
     
            if ($validator->fails()) {
                return redirect('post/create')
                            ->withErrors($validator)
                            ->withInput();
            }
    

    so your code is going to be:

    <?php
    
    namespace AppHttpControllersApi;
    
    use AppModelsUser;
    use IlluminateHttpRequest;
    use IlluminateSupportCarbon;
    use AppHttpControllersController;
    use IlluminateSupportFacadesِAuth;
    use IlluminateSupportFacadesHash;
    use IlluminateSupportFacadesValidator;
    
    
    class UserController extends Controller
    {
        /**
         * Create User
         * @param Request $request
         * @return Response
         */
    
        public function createUser(Request $request)
        {
            try {
    
                 $validateUser = Validator::make($request->all(),
                [      
                    'name'=> 'required',
                    'email' => 'required|email|unique:users,email',
                    'password' => 'required'
                ]);
    
                if($validateUser->fails()){
                    return response()->json([
                        'status' => false,
                        'message' => 'validation error',
                        'errors' => $validateUser->errors()
                    ], 401);
                }
    
              $user = User::ceate([
                'name' => $request->name,
                'message' => $request->email,
                'password' => $request->password
              ]);
               
                return response()->json([
                    'status' => true,
                    'message' => 'User Created Succssfully',
                    'token' => $userInfo
                ], 200);
    
            } catch(Throwable $th){
                return response()->json([
                    'status' => false,
                    'message' => $th->getMessage()
                ], 401);
            }
        }
    
    }
    

    See Laravel documentation for more information.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search