Laravel方法Illuminate\\Validation\\Validator::false不存在。

huangapple go评论62阅读模式
英文:

laravel Method Illuminate\\Validation\\Validator::false does not exist

问题

I can help you with the translation:

(我是新手使用 Laravel)
Postman API 测试一直出现以下错误:
```plaintext
"status": false,
"message": "Illuminate\\Validation\\Validator 类中不存在 false 方法。"

用于注册新用户测试的函数:

<?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
{
    /**
     * 创建用户
     * @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' => '验证错误',
                    'errors' => $validateUser->errors()
                ], 401);
            }

          $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => $request->password
          ]);
           
            return response()->json([
                'status' => true,
                'message' => '用户创建成功',
                'token' => $userInfo
            ], 200);

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

}

请问有人可以帮我吗?我使用了 use Validator; 但仍然收到相同的错误消息!


<details>
<summary>英文:</summary>
(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 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-&gt;all(),
[      
&#39;name&#39;=&gt; &#39;required&#39;,
&#39;email&#39; =&gt; &#39;required|email|unique:users,email&#39;,
&#39;password&#39; =&gt; &#39;required&#39;
]);
if($validateUser-&gt;false()){
return response()-&gt;json([
&#39;status&#39; =&gt; false,
&#39;message&#39; =&gt; &#39;validation error&#39;,
&#39;errors&#39; =&gt; $validateUser-&gt;errors()
], 401);
}
$user = User::ceate([
&#39;name&#39; =&gt; $request-&gt;name,
&#39;message&#39; =&gt; $request-&gt;email,
&#39;password&#39; =&gt; $request-&gt;password
]);
return response()-&gt;json([
&#39;status&#39; =&gt; true,
&#39;message&#39; =&gt; &#39;User Created Succssfully&#39;,
&#39;token&#39; =&gt; $userInfo
], 200);
} catch(\Throwable $th){
return response()-&gt;json([
&#39;status&#39; =&gt; false,
&#39;message&#39; =&gt; $th-&gt;getMessage()
], 401);
}
}

}


can any one help me i use (use Validator;) and still get same error !!
</details>
# 答案1
**得分**: 0
使用`$validateUser->fails()`代替`$validateUser->false()`。
<details>
<summary>英文:</summary>
Use `$validateUser-&gt;fails()` instead of `$validateUser-&gt;false()`
</details>
# 答案2
**得分**: 0
I believe you are looking for the translation of the provided code snippets. Here they are:
```php
我相信你正在寻找来自Laravel文档中的失败,而不是false:
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
{
/**
* 存储新博客文章。
*/
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();
}
}
}

所以你的代码将是:

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
{
    /**
     * 创建用户
     * @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' => '验证错误',
                    'errors' => $validateUser->errors()
                ], 401);
            }

          $user = User::create([
            'name' => $request->name,
            'message' => $request->email,
            'password' => $request->password
          ]);
           
            return response()->json([
                'status' => true,
                'message' => '用户创建成功',
                'token' => $userInfo
            ], 200);

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

更多信息,请参阅 Laravel文档


<details>
<summary>英文:</summary>
I believe you are looking for fails, not false, from the Laravel documentation:
```php
&lt;?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-&gt;all(), [
&#39;title&#39; =&gt; &#39;required|unique:posts|max:255&#39;,
&#39;body&#39; =&gt; &#39;required&#39;,
]);
if ($validator-&gt;fails()) {
return redirect(&#39;post/create&#39;)
-&gt;withErrors($validator)
-&gt;withInput();
}

so your code is going to be:

&lt;?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-&gt;all(),
            [      
                &#39;name&#39;=&gt; &#39;required&#39;,
                &#39;email&#39; =&gt; &#39;required|email|unique:users,email&#39;,
                &#39;password&#39; =&gt; &#39;required&#39;
            ]);

            if($validateUser-&gt;fails()){
                return response()-&gt;json([
                    &#39;status&#39; =&gt; false,
                    &#39;message&#39; =&gt; &#39;validation error&#39;,
                    &#39;errors&#39; =&gt; $validateUser-&gt;errors()
                ], 401);
            }

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

        } catch(\Throwable $th){
            return response()-&gt;json([
                &#39;status&#39; =&gt; false,
                &#39;message&#39; =&gt; $th-&gt;getMessage()
            ], 401);
        }
    }

}

See Laravel documentation for more information.

huangapple
  • 本文由 发表于 2023年6月6日 17:24:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76413199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定