0xMinCha
0xMinCha

Reputation: 416

Lumen "PUT" request doesn't update

I've the below code snippet to update a MySQL table. I'm testing my back-end with POSTMAN PUT request. This code doesn't seem to update MySQL correctly, instead it sets the MySQL cells to empty values, even though I'm getting "Updated Successfully" in my POSTMAN result.

Below is the Lumen PHP code

public function updateSensorPackage(Request $request, $id){
   $sensorPackage = AddSensorPackage:: find($id);
   $sensorPackage -> elderly_id = $request ->  input('elderly_id');
   $sensorPackage -> centre_id = $request -> input('centre_id'); 
   $sensorPackage -> package_id = $request -> input('package_id'); 
   $sensorPackage -> beacon_id = $request ->  input('beacon_id');
   $sensorPackage -> created_by = $request -> input('created_by'); 
   $sensorPackage -> save();
   return response('Updated Successfully', 200);

}

I set the POSTMAN header to "Content-type : Application/json" and sending the parameters in request body. Below I've pasted the POSTMAN "PUT" request.

enter image description here

After running dd($request->all()); I got the below result

enter image description here

My PHP version is 7+ and Lumen version is 5.5.2

Upvotes: 3

Views: 4123

Answers (3)

Somwang Souksavatd
Somwang Souksavatd

Reputation: 5085

Unlink get method your need to define header

Accept: 'application/json'
'Content-Type': 'application/json'

Upvotes: 1

Shahrukh Anwar
Shahrukh Anwar

Reputation: 2632

Write your parameters in

x-www-form-urlencode 

and see the magic, it works fine.The reason is laravel just create a feel of PUT request, it can't be given in form-data or raw data

Upvotes: 11

Borjante
Borjante

Reputation: 10497

You should try sending a POST request instead of PUT, and then later add a new param like this to the request.

_method = "PUT"

I can't remember where I found this but it has something to do with the underlying symfony request class.

EDIT: Found it.

You can also set Postman to send request parameters using 'x-www-url-formurlencoded'

https://laravel.io/forum/02-13-2014-i-can-not-get-inputs-from-a-putpatch-request

Upvotes: 3

Related Questions