My bad! In postman I was sending the data as "form-data". Changing the option of the data type to "x-www-form-urlencoded" fixed the issue :)
Mar 26, 2018
3
Level 1
PUT request values are empty
I am using Lumen for my API. I have a PUT method for updating a product. However, the data is not available in the request object. Why is that? I've seen a few mentions of having to "fool" it by adding _method = put
Surely for an API framework, PUT should work out the box? or am i not understanding?
Thanks
$router->post('/products', 'ProductController@create');
$router->get('/products', 'ProductController@list');
$router->put('/products/{id}', 'ProductController@update');
$router->get('/products/{id}', 'ProductController@show');
controller:
public function update($id, Request $request)
{
$product = $this->productService->editProduct($id, $request);
if (! $product) {
return response()->json(['success' => false], 500);
} else {
return response()->json(['success' => true], 200);
}
}
service
public function editProduct($id, $request)
{
$product = Product::find($id);
$product->name = $request->input('name');
$product->description = $request->input('desc');
if ($product->save()) {
return true;
} else {
return false;
}
}
Level 1
10 likes
Please or to participate in this conversation.