简体   繁体   中英

How to use patch request in Laravel?

There is entity User that is stoted in table Users

Some fields in this table are null by default.

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

For this I try to use PATCH method in Laravel:

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

    }

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

How to use right handler for PATCH method in Laravel?

your route is:

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

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

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

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

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

if you don't use ajax than use following:

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

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>

Update the routing as per below

Route::patch('/users/update/{id}',[
    'uses' => 'UsersController@update'
]);

Yes, you need to send id for route patch. Example from https://laravel.com/docs/5.4/controllers#resource-controllers for Laravel

PUT/PATCH - /photos/{photo}, so you don't need update word in your route. Just users/id and methods PUT or PATCH.

UPD for CRUD operations:

// Routes
Route::resource('items', 'ItemsController');

// Form for update item with id=1
<form method="POST" action="{{ route('items.update', ['id' => 1])}}">
    {!! csrf_field() !!}
    <input name="_method" type="hidden" value="PATCH">
    <!-- Your fields here -->
</form>

// Controller
public function update($id, Request $request)
{
    // Validation here

    $item = Item::findOrFail($id);

    // Update here
}
<form method="POST" action="{{url('admin/forms',['id' => $form->domain_id])}}">
@csrf
@method('PATCH')

route:

    Route::resources([
    'forms' => FormsController::class,
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM