3
votes

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

3
try $sensorPackage->update(); insteadHussein
@Hussein, not working :)CoolCK
keep this change and access the properties like this $request->elderly_idHussein
Does the AddSensorPackage's $fillable property contains an array with all the fields you want to update?Ivanka Todorova
@IvankaTodorova $fillable has to do with mass assignment, which is not his caseHussein

3 Answers

9
votes

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

2
votes

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

1
votes

Unlink get method your need to define header

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