无法更新 registration_id

使用Postman更新registration_id,提示:

{
    "message": "The given data was invalid.",
    "errors": {
        "name": [
            "用户名不能为空。"
        ],
        "email": [
            "邮箱 不能为空。"
        ]
    }
}

看了UserRequest.php文件,如果不提交nameemail在设定上确实会报错,为何课程截图用Postman可以成功修改registration_id

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

检查UserRequest.php的patch部分是否写对了:

.
.
.
case 'PATCH':
        $userId = auth('api')->id();

        return [
            'name' => 'between:3,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,name,'.$userId,
            'email' => Rule::unique('users')->ignore($userId),
            'introduction' => 'max:80',
           'avatar_image_id' => Rule::exists('images', 'id')->where(function($query) use ($userId){
                $query->where('type', 'avatar')->where('user_id', $userId);
            })
        ];
        break;
.
.
.

nameemail都没有设required

4年前 评论
讨论数量: 4

检查UserRequest.php的patch部分是否写对了:

.
.
.
case 'PATCH':
        $userId = auth('api')->id();

        return [
            'name' => 'between:3,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,name,'.$userId,
            'email' => Rule::unique('users')->ignore($userId),
            'introduction' => 'max:80',
           'avatar_image_id' => Rule::exists('images', 'id')->where(function($query) use ($userId){
                $query->where('type', 'avatar')->where('user_id', $userId);
            })
        ];
        break;
.
.
.

nameemail都没有设required

4年前 评论

@tsin larabbs的L03_6.x分支中,UserRequest.phpnameemail有设置了required

public function rules()
    {
        return [
            'name' => 'required|between:3,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,name,' . Auth::id(),
            'email' => 'required|email',
            'introduction' => 'max:80',
            'avatar' => 'mimes:jpeg,bmp,png,gif|dimensions:min_width=208,min_height=208',
        ];
    }
4年前 评论

@SeanSolomon 5.2. 编辑个人资料《L03 Laravel 教程 - 实战构架 API 服务器 ( Laravel 6.... 看这节,没有加required,加了required的参数没有传当然会返回相应的验证错误提示。

4年前 评论

@tsin 按这样修改可以。谢谢

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!