PHP - Lumen: update returns true but does not update the data in the database

Asked

Viewed 119 times

1

I’m a beginner in PHP - Lumen and I’m doing my first api. All my methods in the controller are working, except the update. Even if it returns true, the data is not changed in the database.

public function update(Request $request, $id)
{
    $agendamento = $this->model->find($id)
        ->update($request->all());

        return response()->json($agendamento);
}

I already checked and $request is bringing the data correctly.

  • Is there a change to be made? How is your object before and after sending the request? How is the request data?

  • 1 - GET/2 {
 "Id": 2,
 "IdSala": 2,
 "EmailRequisitante": "[email protected]",
 "DataHoraInicio": "2019-12-06 15:00:00",
 "DataHoraFim": "2019-12-06 16:00:00",
 "Situacao": "Avitva",
 "Descricao": "Teste data e hora"
} 2 - PUT/2 {
 "Id": 2, 
 "IdSala": 2,
 "EmailRequisitante": "[email protected]",
 "DataHoraInicio": "2019-12-06 15:00:00",
 "DataHoraFim": "2019-12-06 16:30:35",
 "Situacao": "Avitva",
 "Descricao": "Teste data e hora MODIFICADO"
} After PUT, when making a new GET, the returned data remains as in the first situation.

  • My mapping $router->put("/{id}", "AgendamentoController@update");

1 answer

0

Ana, I have no experience in Lumen but in your father Laravel I handle the data before saving or updating, I never call $request inside the update but before mapped the data;

 $stockTeam = new StockTeam();
                $stockTeam->idTeam = $request->idTeam;
                $stockTeam->idSupplies = $request->idSupplies;
                $stockTeam->amount = $request->amount;
                $stockTeam->save();

I hope I’ve helped, this is the way I’ve worked with Laravel I’ve done it too:

 DB::insert('insert into address_transfers
    ( name, type_id, plan_id, neighborhood_id, status_id, dot, user_id, description, created_at, updated_at)
        VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, current_timestamp, ?)',
     [ $request->name, $request->type, $request->plans, $request->hood, $request->status, $request->dot, Auth::id(), $request->description, $request->date ]);
  • i tried to map the request data and it still didn’t work.

  • I spent most of the day reading this. I found that Lumen "tricks" the PUT and PATCH requests and when calling in Postman you need to put the parameters in 'x-www-url-formurlencoded'. But I’m not sure what the syntax would be like to put the whole object being sent inside the request.

  • Ana, see if these two links can help you. https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data https://dev.to/sidthesloth92/understanding-html-form-encoding-url-encoded-and-multipart-forms-3lpa

  • what solved for me was to put the names of the parameters of the model all in minusculo

  • I’m glad you made it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.