4

Is it possible to make Ajax PATCH requests to Laravel, or am I restricted to POST? Laravel uses PATCH in input hidden fields. However, I am not using form elements—just buttons that should partially update a record when clicked (via an Ajax request).

How would the route look like for this?

Routes file

Route::patch('questions/{id}', 'QuestionController@update')->before('admin');

I am not sure if Laravel routes support PATCH.

Controller

public function update($id) {
    if (Request::ajax() && Request::isMethod('patch')) {
        //partially update record here
    }
}

JavaScript

$('div#question_preview <some button selector>').click(function (event) {
    $.ajax({
        url: 'questions/' + question_id,
        type: 'PATCH',
        data: {status: 'some status'}
    });
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rafael
  • 7,605
  • 13
  • 31
  • 46
  • 1
    @SetKyarWaLar PUT is used if you know the entire contents of the resource. In my case, I am only updating a part of the resource without the entire resource data which is what patch is for. – Rafael Jan 13 '15 at 04:05
  • Can you describe your view in your question? @Rafael – Set Kyar Wa Lar Jan 13 '15 at 04:06

2 Answers2

11

Yeah, it's possible. Try in your JavaScript:

$('#div#question_preview <some button selector>').click(function() {
    $.ajax({
        url: 'questions/'+question_id,
        type: 'PATCH',
        data: {status: <SOME VALUE I WANT>, _method: "PATCH"},
        success: function(res) {
        }
    });
});

In your route:

Route::patch('questions/{id}', 'QuestionController@update')->before('admin');

In your QuestionController Controller's update method:

dd(Request::method());

You will see the response like

string(5) "PATCH"

Read more about request information in the Laravel documentation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Set Kyar Wa Lar
  • 4,488
  • 4
  • 30
  • 57
  • :D Thank you man, you are awesome! Also I did not include the `_method: "PATCH"` in my data since it is already being requested as patch and it worked without any issues. Thank you for your help, cheers! – Rafael Jan 13 '15 at 04:59
  • Quick question but how do I access the status values (in my data) on the server side? Usually I would use $_GET or $_POST supers but for patch? – Rafael Jan 13 '15 at 05:06
  • For Laravel `Request::isMethod('PATCH')` ? – Set Kyar Wa Lar Jan 13 '15 at 05:11
  • I mean to access my data in the ajax call? I want to access the key / value pair I stored in the jQuery data method. – Rafael Jan 13 '15 at 05:14
  • 1
    `Input::all()` for all of your input values. For specific name `Input::get('specific-name');` Read more on [doc](http://laravel.com/docs/4.2/requests#basic-input) – Set Kyar Wa Lar Jan 13 '15 at 05:22
1

It's possible!

You should need to provide an extra parameter in the form request called as _method with a value PATCH.

JS

$('div#question_preview <some button selector>').click(function (event) {
    $.ajax({
        url: 'questions/'+question_id,
        type: 'PATCH',
        data: {status: 'some status',_method: 'PATCH'}
    });
});

You can provide a hidden input in the view file with the value PATCH for better readability HTML

<input type="hidden" name="_method" value="PATCH">

If you are using FormData, you need to send the request as POST. The Laravel application will automatically get it as a PATCH request because you included @method('PATCH'). So your route and method for that will get triggered.

JS with FormData

$('div#question_preview <some button selector>').click(function (event) {
    let form_data= new FormData();
    form_data.append('status','some status');
    form_data.append('_method','PATCH');
    $.ajax({
        url: 'questions/'+question_id,
        type: 'POST',
        data: form_data
    });
});
Nishal K.R
  • 1,090
  • 11
  • 21