Carbon in Laravel updates wrong record

3

I have a problem in the controller with the UPDATE, my STORE is as follows:

public function store(Request $request)
{
    $date = Carbon::now('America/Asuncion')->toDateTimeString();
    $cabcaja = new cab_caja($request->all());
    $cabcaja->fecha_inicio = $date;
    $cabcaja->estado = 1;
    $cabcaja->save();
    Flash::success("Caja abierta en fecha");
    return redirect()->route('caja_actual.index');
}

There he keeps everything perfect. But when updating the bd using the UPDATE:

public function update(Request $request, $id)
{
    $datefin = Carbon::now('America/Asuncion')->toDateTimeString();
    $cabb_caja = cab_caja::find($id);
    $cabb_caja->fecha_fin = $datefin;
    $cabb_caja->estado = 0;
    $cabb_caja->fill($request->all());
    $cabb_caja->save();
    Flash::warning('La caja ha sido cerrada.');
    return redirect()->route('index');
}

I update the field "start_date" to the current date (the same as end_date). I'm using Laravel in its version 5.1

dd ($ request-> all ()):

array:6 [▼ "_method" => "PUT" "_token" => "TgjPpopkNFjD8vxbwc1gVZo6wTSa1nI36ZqmsNsR" "gs_cierre" => "9443" "pe_cierre" => "3967" "gs_arreglo" => "100" "pe_arreglo" => "399" ] –

Array attributes before save ():

#attributes: array:12 [▼
"id" => 15
"fecha_inicio" => "2016-07-29 00:37:43"
"fecha_fin" => "2016-07-29 00:43:13"
"gs_apertura" => 5999.0
"pe_apertura" => 3334.0
"gs_cierre" => "9443"
"pe_cierre" => "3967"
"estado" => 0
"gs_arreglo" => "0"
"pe_arreglo" => "0"
"created_at" => "2016-07-29 04:37:43"
"updated_at" => "2016-07-29 04:37:43"
]
    
asked by Marcos Ledezma 29.07.2016 в 06:30
source

1 answer

1

What I did was make a "bridge" to save (). I saved the start_date in an auxiliary variable, in the first save () it updates the return_date_date field but immediately created a second instance of cab :: box with the sole purpose of passing this auxiliary variable to start_date and updating it once more. Annex as it was finally my UPDATE:

public function update(Request $request, $id)
{
    $datefin = Carbon::now('America/Asuncion')->toDateTimeString();
    $cabb_caja = cab_caja::find($id);
    $fechain = $cabb_caja->fecha_inicio;
    $cabb_caja->fecha_fin = $datefin;
    $cabb_caja->estado = 0;
    $cabb_caja->fill($request->all());
    $cabb_caja->save();
    $cabb_caja2 = cab_caja::find($id);
    $cabb_caja2->fecha_inicio = $fechain;
    $cabb_caja2->save();
    Flash::warning('La caja ha sido cerrada.');
    return redirect()->route('index');
}

With this it was solved, although I was intrigued by the reason why in the first save () I updated the field start date

    
answered by 29.07.2016 в 07:06