英文:
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->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 !!
</details>
# 答案1
**得分**: 0
使用`$validateUser->fails()`代替`$validateUser->false()`。
<details>
<summary>英文:</summary>
Use `$validateUser->fails()` instead of `$validateUser->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
<?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();
}
so your code is going to be:
<?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);
}
}
}
See Laravel documentation for more information.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论