laravel方法Illuminate\\Validation\\Validator::false不存在[关闭]

uwopmtnx  于 2023-06-07  发布在  其他
关注(0)|答案(2)|浏览(238)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答复。

此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
13小时前关闭
Improve this question
(im新的laravel) Postman API测试错误这样的所有时间

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

注册新用户进行测试功能

<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\ِAuth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

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);
        }
    }

}

可以任何一个帮助我我使用(使用验证器;)并且仍然得到相同的错误!!

yqkkidmi

yqkkidmi1#

使用$validateUser->fails()代替$validateUser->false()

n9vozmp4

n9vozmp42#

我相信你正在寻找失败,而不是错误,从Laravel文档:

<?php
 
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
 
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();
        }

所以你的代码将是:

<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\ِAuth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

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);
        }
    }

}

请参阅Laravel文档以获取更多信息。

相关问题