Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 653
Darama Avatar asked Feb 18 '17 08:02

Darama


People also ask

What does PATCH do in laravel?

PATCH = update the resource with the values provided and return the updated resource, this returns the resource as the PATCH method MAY update values not originally provided in the request, or perform other actions on the data.

What is the difference between put and PATCH in laravel?

Put is when you update the whole model and use patch when you want to update a portion or single attribute. For instance, a user may have a username they change that is stored on the user model. You would patch just the username when they change it.

When should I use put and PATCH?

The PATCH method is the correct choice here as you're updating an existing resource - the group ID. PUT should only be used if you're replacing a resource in its entirety.

What is the difference between put and PATCH?

PUT is a method of modifying resource where the client sends data that updates the entire resource . PATCH is a method of modifying resources where the client sends partial data that is to be updated without modifying the entire data.


3 Answers

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", ...}
});

if you don't use Ajax then use the following:

<form method="POST" action="{{ route('users.update',['id' => $id]) }}">
    @csrf
    @method('PATCH')
</form>
like image 142
Jigneshsinh Rathod Avatar answered Sep 21 '22 02:09

Jigneshsinh Rathod


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
}
like image 28
aleksejjj Avatar answered Sep 22 '22 02:09

aleksejjj


Update the routing as per below

Route::patch('/users/update/{id}',[
    'uses' => 'UsersController@update'
]);
like image 20
Pramod Patil Avatar answered Sep 21 '22 02:09

Pramod Patil