Prevent saving the form twice?

0

As I prevent a record from being saved or updated, it can no longer be modified, because when you go back with the browser you can modify the information. For the moment, I have this:

if($deposito = Deposito::lastest()
    ->where('create', '=','true'))
{
    return redirect()->route('depositos.index');
}
else if($deposito = Deposito::lastest()
    ->where('create','=','false'))
{
$deposito = new Deposito()
$deposito->noboleta = $request->noboleta
$deposito->monto = $request->monto
$deposito->id_tipo = $request->id_tipo
$deposito->id_banco = $request->id_banco
$deposito->id_estado = $request->id_estado
$deposito->fechaboleta = $request->fechaboleta
$deposito->detalles = $request->detalles
$deposito->id_usuario = Auth::id()
$deposito->create = true
$deposito->save()
return redirect()->route('depositos.index')
    ->with('success','Formulario Guardado');
}

}

But it generates an error, when placing; or just a comma.

    
asked by JoséOrdoñez 14.12.2017 в 18:54
source

2 answers

2

Conclusions of what was said in the Chat .

The first thing is to make sure that the records can not be duplicated in our database, for that we use the firstOrCreate method that provides us Eloquent , the ORM of Laravel

public function store(DepositoRequest $request) {
    Deposito::firstOrCreate([
    'noboleta' => $request->noboleta,
    'monto' => $request->monto,
    'id_tipo' => $request->id_tipo,
    'id_banco' => $request->id_banco,
    'id_estado' => $request->id_estado,
    'fechaboleta' => $request->fechaboleta,
    'detalles' => $request->detalles,
    'id_usuario' => Auth::id(),
    'create' => true
    ]);
}

Then we must do a validation at the time of making update through a field booleano so that the record can not be edited in almost that it was edited before:

public function update(EditRequest $request, $id){ 
    $deposito = Deposito::findOrFail($id);

    if ($deposito->update) {
        return redirect()->route('depositos.autorizacion')->with('success', 'Deposito no puede ser actualizado');     
    }else{
        $deposito->id_estado = $request->id_estado; 
        $deposito->observaciones = $request->observaciones; 
        $deposito->autorizacion = Auth::user()->name; 
        $deposito->cod_autorizacion = Uuid::generate()->string; 
        $deposito->update = true; 
        $deposito->save(); 
        return redirect()->route('depositos.autorizacion')->with('success', 'Deposito actualizado'); 
    }
}

I do not recommend trying to block the user's navigation to prevent it from being returned (in fact I do not know if it is possible) but damaging the navigability of the site seems to me to be not optimal for resolving this type of conflicts, it is better to apply some logic of validation and by means of emergent messages to let the user know what happened.

    
answered by 15.12.2017 / 16:27
source
0

By allowing the browser to go back, you run the risk of saving twice, or more, the same data with a different id, since the form does not have an id that identifies the data.

I can think of two options:

1.- When you save the data do a search in the database for all the fields of the form (those that are identifying the registry) and in case the query returns a record you do an update or simply, nothing; otherwise you create a new record.

2.- Do not allow to go back in the browser using javascript.

    
answered by 15.12.2017 в 11:59