skip to Main Content

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
When passing data from query parameters

vs When passing data from body
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?

For anybody who want to see headers passed
headers

2

Answers


  1. enter image description here

    try to send put request like this, it may works.

    Login or Signup to reply.
  2. You can use post() to get body parameters

    $datarequest = $this->input->post(); // this will contain all body parameters
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search