Reputation: 2966
I try to update data in laravel with ajax with put
method, when I echo request input the result is null, this is my ajax
let formData = new FormData();
formData.append('name', 'lorem ipsum'); // I want get this
$.ajax({
url : 'product/17',
method : 'put',
data: formData,
contentType: false,
processData: false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success : function(res){
alert(res); // result is null
}
});
Route :
Route::resource('/product', 'ProductController');
Controller :
public function update(Request $request, $id){
echo $id; // result is 17
echo $request->input('name'); // result is null :(
}
I have been search and try other code but not work, how to solve it? thanks
Upvotes: 1
Views: 919
Reputation: 9359
I think may be it's because of method of request. There is different way of sending put request than normal get/post request. You can modify like this:
let formData = new FormData();
formData.append('name', 'lorem ipsum'); // I want get this
formData.append('_method', 'put'); // Specify method
$.ajax({
url : 'product/17',
method : 'post',
data: formData,
contentType: false,
processData: false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success : function(res){
alert(res); // result is null
}
});
Now, You can access data with the same approach.
$request->input('name');
I hope you understand.
Upvotes: 1