久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 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 中使用補丁請求?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Magento products by categories(按類別劃分的 Magento 產品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶帳戶注冊事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 99精品免费视频 | 午夜精品久久 | 黄片毛片免费观看 | 成人久久网| 日韩中文一区二区三区 | 日韩一区二区三区视频在线播放 | 人人看人人爽 | 国产成人一区二区 | 国产精品黄视频 | 欧美日韩在线一区二区 | 天堂视频中文在线 | 日韩免费视频 | 狠狠操你 | 一级片在线观看视频 | 成人网av| 亚洲不卡在线观看 | 99成人精品 | 99精品国产一区二区青青牛奶 | 国产精品久久久久久婷婷天堂 | www.久久.com | 亚洲在线一区二区 | 日韩视频一区二区 | 三级黄视频在线观看 | 日本成人中文字幕在线观看 | 91亚洲免费| 国产精品美女一区二区 | 精品国产乱码久久久久久果冻传媒 | 国产小视频在线 | 91精品国产色综合久久 | 精精久久| 能看的av网站| 国产精品日韩一区二区 | 日韩亚洲一区二区 | 成人午夜免费福利视频 | 欧美日韩在线一区二区 | 亚洲一区在线日韩在线深爱 | 国产在线中文 | 亚洲自拍偷拍欧美 | 激情一区二区三区 | 在线视频亚洲 | 日韩成人一区 |