There is entity User that is stoted in table Users

此表中的某些字段默认为空.

I need to update these fields and set not null data.

为此,我try 在Laravel中使用PATCH方法:

Routing:

Route::patch('users/update', 'UsersController@update');

Controller:

public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            "name" => 'required|string|min:3|max:50',
            "email_work" => 'email|max:255|unique:users',
            "surname" => 'required|string|min:3|max:50',
            "tel" => 'required|numeric|size:11',
            "country" => 'required|integer',
            "region" => 'required|integer',
            "city" => 'required|integer'
        ]);

        if ($validator->fails()) {
            return response()->json(["message" => $validator->errors()->all()], 400);
        }

        $user = User::where("user_id", $id)->update([
            "name" => $request->name,
            "surname" => $request->surname,
            "tel" => $request->tel,
            "country" => $request->country,
            "city" => $request->city,
            "region" => $request->region,
            "email_work" => $request->email
        ]);

        return response()->json(["user" => $user]);

    }

这是否意味着我可以传递任何数据进行更新?

如何在Laravel中使用正确的补丁方法处理程序?

推荐答案

If your route is:

Route::patch('users/update/{id}', 'UsersController@update')->name('users.update');

if you use jQuery.ajax() for submitting data then set your method, data, and URL like this:

$.ajax({
    method: "post",
    url: "{{ url('/users/update/') }}" + id,
    data: {"_method": "PATCH", ...}
});

如果您不使用Ajax,那么可以使用以下内容:

<form method="POST" action="{{ route('users.update',['id' => $id]) }}">
    @csrf
    @method('PATCH')
</form>

Laravel相关问答推荐

在Laravel中URL中添加保护ID

如何解决此 Backblaze B2 S3 兼容 API 错误?

函数接受 Laravel 集合对象,尽管只允许使用字符串

Laravel Homestead vagrant up 超时

如何使用 Laravel 查询生成器在 WHERE 条件周围添加括号

Laravel 集合排序不生效

Laravel 在维护模式下显示自定义消息

Laravel 5.1 - 如何在进度条上设置消息

Laravel 5.7 判断Electron邮件是否经过验证

在 Laravel 中无效后防止重定向到主页

php Laravel-遇到格式不正确的数值(在字符串上)

调用未定义的方法 Maatwebsite\Excel\Excel::load()

Laravel assets资源与混合

Laravel 更新语法 - 使用数组更新记录

Laravel:验证 json 对象

Laravel 5.4 Vue.JS 无法挂载组件:未定义模板或渲染函数

Laravel 5 new auth:获取当前用户以及如何实现角色?

将参数传递给 Laravel 中的中间件

Laravel 5.2 验证错误未出现在Blade中

如何访问 Laravel 集合中的第 n 个项目?