PATCH AJAX request in Laravel

Can I make ajax patch request to laravel? Or do I have to strictly use post? I understand that Laravel uses a patch on hidden fields in form elements. However, I am not using form elements, just buttons that should partially update the record when clicked via ajax.

What does the route look like for this?

Routes file

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

I'm not sure if Larvel Routes support HTTP patch verb in routes file.

controller

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

      

JS FILE

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

      

Just look for clarity here, thanks!

+3


source to share


1 answer


Yes, it is possible to 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 controller update method QuestionController

dd(Request::method());

      

You will see the answer as

string(5) "PATCH"

      

More on requesting information on Laravel doc .

+7


source







All Articles