如何在 Laravel 中使用补丁请求?

How to use patch request in Laravel?(如何在 Laravel 中使用补丁请求?)
本文介绍了如何在 Laravel 中使用补丁请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

Users

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

Some fields in this table are null by default.

我需要更新这些字段并设置非空数据.

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

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

For this I try to use PATCH method in Laravel:

路由:

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

控制器:

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

    }

这是否意味着我可以传递任何数据进行更新?我应该将 $id 参数相对传递给路由和控制器吗?

Does it mean that I can pass any data to update? Should I pass $id parameter to routing and controller relatively?

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

How to use right handler for PATCH method in Laravel?

推荐答案

你的路线是:

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

用以下用于所有 CRUD 操作的路由替换你的路由:

replace your route with following route that use for all CRUD opration:

Route::resource('users', 'UsersController');

如果您使用 ajax 提交数据,则将您的类型和网址替换为以下内容:

if you use ajax for submit data then replace your type and url with following:

type: "patch",
url: "{{url('/')}}users/" + id,

如果您不使用 ajax,请使用以下内容:

if you don't use ajax than use following:

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

更新:在 5.6 版之后,您可以在任何刀片文件中对上述函数使用这些语法:

update: after version 5.6 you can use these syntax for above functions in any blade file:

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

这篇关于如何在 Laravel 中使用补丁请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)