0

I have to porting a web site in laravel so in my old project i call this query:

$username=mysqli_real_escape_string($conn,$_SESSION['username']);
    $nome=mysqli_real_escape_string($conn,$_POST['titolo']);
    $query="insert into iscrizioni values('$username','$nome');";
    $res=mysqli_query($conn,$query) or die(mysqli_error($conn));
    if($res===TRUE){
        echo 1;
    }else{
        echo mysqli_error($conn);
    }

But now in my Controller :

public function iscrizione(){
            $data=request();
            $titolo=$data['titolo'];
            $user=session('user_id');
            $newIscrizione=Iscrizione::create([
                'user'=>$user,
                'corso'=>$titolo,
            ]);
            if($newIscrizione){
                return 1;
            }else{
                return 
            }
        }

I created some triggers in my DB with a specific sqlstate_message for each so i need to return it in my js file. How can i do this?

1 Answer 1

3

You can use try catch exception handling

 try{
       $newIscrizione=Iscrizione::create([
                'user'=>$user,
                'corso'=>$titolo,
            ]);
    }catch (\Illuminate\Database\QueryException $exception){

        dd($exception->getMessage());
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.