Basically what my controller does is update the data from database. However when testing the api for the controller, the data passed through query parameters works while data passed from body doesn’t.
For eg: When passing data from query parameters
vs When passing data from body
My controller looks like this
public function update(Request $request, $id)
{
if (Contact::where('id', $id)->exists()) {
$editedContactData = Contact::find($id);
$editedContactData->province = is_null($request->province) ? $editedContactData->province : $request->province;
$editedContactData->district = is_null($request->district) ? $editedContactData->district : $request->district;
$editedContactData->local = is_null($request->local) ? $editedContactData->local : $request->local;
$editedContactData->spokesman = is_null($request->spokesman) ? $editedContactData->spokesman : $request->spokesman;
$editedContactData->phone = is_null($request->phone) ? $editedContactData->phone : $request->phone;
$editedContactData->email = is_null($request->email) ? $editedContactData->email : $request->email;
$editedContactData->save();
return response()->json([
"message" => "Contact Updated successfully",
"editedContactData" => $editedContactData
], 201);
}else{
return response()->json([
"message" => "Contact Not Found."
], 404);
}
}
I think there is problem with my controller, yet I’m unable to find the solutions. Any problem the code might have?
2
Answers
try to send put request like this, it may works.
You can use
post()
to get body parameters