I have a "create" method
public function crear(Request $req) {
$rules = array(
'nombre' => 'required',
);
$validator = Validator::make ( Input::all (), $rules );
if ($validator->fails()){
return Response::json(array('errors' => $validator->getMessageBag()->toArray()));
}else{
try{
$cargos = new Cargos();
$cargos->nombre = $req->nombre;
$cargos->save();
return response()->json($cargos);
}catch(\Illuminate\Database\QueryException $e){
return $e->getBindings();
}
}
}
I get the value with jquery
success: function(data) {
if((data.errors)){
alert(data.errors.nombre);
}else{
if(data){
alert("Error: "+data);
}else{
VolverANumerar();
}
}
},
but that code throws this at me, when I try to register a value that is already registered in a table and the field has a unique type restriction:
Error: Operator, 2016-11-01 14: 55: 51,2016-11-01 14:55:51
in the api de laravel I got that getBindings()
throw a code served me with this:
try{
Cargos::find($req->id)->delete();
return response()->json();
}catch(\Illuminate\Database\QueryException $e){
return $e->getBindings();
}
but with the first one not.
What I want is that in all the Exception of try / catch I throw a code .. is to not show the user a sql error but something that he can understand. with the first code I try to register an item that is already registered and returns the data that I try to register, but it does not return a code as such for me to validate it. In the last code that I show I delete an item that is being used in another table and the Exception throws a 1 .. I validate it on the screen, if it is 1 then tell the user that he is trying to delete an item that is used. / p>
I do not know if I still can not explain myself. Excuse my awkwardness for not knowing how to explain myself well. First time I comment on a forum xD
Thank you in advance for the help.