I have the following problem, I am creating an employee checker to record their entry and exit, I am using coal to obtain the date and time, previously by school regulations I had made the modification of the record through a procedure, but it is the first time that I am using ELOQUENT to perform this operation, it sends me the record to modify and everything is fine, the problem comes when I give it submit and the record is duplicated creating a new one with the exit date and I don't know why, I attach the code of my controllers and of my route
public function edit(checadorEmpleado $checadorEmpleado, $id)
{
$salida = checadorEmpleado::find($id);
return view('checadorempleado.edit',['salida'=>$salida]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\checadorEmpleado $checadorEmpleado
* @return \Illuminate\Http\Response
*/
public function update(Request $request, checadorEmpleado $checadorEmpleado)
{
$date5=carbon::now();
$date6=$date5->Format('Y-m-d h:i:s A');
$checadorEmpleado->id = $request['id'];
$checadorEmpleado->empleado_id = $request['empleado_id'];
$checadorEmpleado->hora_entrada= $request['hora_entrada'];
$checadorEmpleado->hora_salida = $date6;
$checadorEmpleado->save();
return redirect('checador');
}
ROUTE
Route::resource('checador','ChecadorEmpleadoController');
Route::post('altacheck','ChecadorEmpleadoController@store')->name('altacheck');
Route::get('/modificachecador/{id}','ChecadorEmpleadoController@edit');
Route::POST('modificachecador','ChecadorEmpleadoController@update')->name('modificachecador') ;
I see some inconsistencies in the code.
First, let's look at the method
edit()
and its corresponding route. In the route, you have a parameter{id}
, which is ok for when, for example, you want to edit the model withid => 1
, you access a uri like this/modificachecador/1
.But in the controller's edit method, you are injecting 2 parameters,
$checadorEmpleado
e$id
.This doesn't make sense if the route has only one parameter, and I'm surprised it's working for you, because the method should just look like this:
Then let's see the method
update()
. The problem seems to be, on the one hand, in the routebecause it doesn't have the parameter , but in the controller
{id}
method , you try to inject that parameter withupdate()
checadorEmpleado $checadorEmpleado
So, although I don't know how you are sending the request to that route, I see at least two possibilities:
And in the controller method inject it as a dependency to get the model to modify
When receiving a form you would no longer receive an instance of checkerEmployee, also it does not appear as a parameter in your routes file, I will make some adjustments to your code
I already found the error, apparently it had some errors in the update function, since it was like
save()
but I changed it toupdate()
, also that at the beginning of my variables I should have put the methodfind()
attached to my code where I made the corrections: