Could someone help me to correct my laravel code so that before inserting a new record, check if that record exists? (to avoid duplicates) that is, if there is a record equal to the one to be inserted, do not allow registration, otherwise allow it to be inserted.
It is worth mentioning that what I want to validate is a foreign column and it arrives as a value in a formula of formulurio. So far I have the following:
$ciclo=$request->idCiclo; //Guardo el valor que recibo del formulario
$existencia = DB::table('planespago') //realizo la sentencia para saber si existe
->select('idCiclo')
->where('idCiclo', '=', $ciclo);
if ($existencia = $ciclo) { //aqui valido si son iguales en el campo de la db y
lo que llego del formulario
return json_encode('no puedes registrar otro');
}else{
return json_encode('creado');
}
But the validation does not do me good and it passes directly that it is true even though I choose a value that does not exist