how can I get the error code in a try catch in laravel?

4

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.

    
asked by Pablo Contreras 01.11.2016 в 18:58
source

1 answer

1

I do not understand exactly the problem, to know if it was possible to save what you should check is the save()

For example:

if($cargos->save()) {
    return Response::json(array('success' => true), 200);
}     
return Response::json(array('error' => true), 200);

Neither do you really need the } else { if the validator fails directly, the return is executed, so you can leave it like this:

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()));
    }

    $cargos = new Cargos();
    $cargos->nombre = $req->nombre;

    if($cargos->save()) {
        return Response::json(array('success' => true), 200);
    }     
    return Response::json(array('error' => true), 200);
}

In either case, if you want to throw an exception when it is not saved correctly, you modify the last return with a throw for the exception and that's it.

Edit: Are you sure you're throwing the exception? Because what you are saying, it does not seem that when you are deleting you are throwing an exception, but that the 1 is because the delete has gone well.

Do you have a capture of Laravel's error?

In any of the cases, the exception you are trying to catch is \Illuminate\Database\QueryException so if you throw another one you will not do catch , or add more or put a more generic so you can take it safe .

    
answered by 02.11.2016 / 12:38
source