giò
giò

Reputation: 3580

How do i know if a transaction went well in Laravel?

Let's say I am using a transaction in Laravel:

DB::transaction(function() {

     // Do your SQL here

});

Is there any way I can know after that if the transaction went well or not?

DB::transaction(function() {

     // Do your SQL here

});

// Here how can I know if the transaction is ok?

Upvotes: 3

Views: 763

Answers (1)

Moppo
Moppo

Reputation: 19275

A transaction is supposed to work as expected unless some component throws an exception during the transaction execution

If you want to check if something specific went wrong you can do:

try
{
    DB::transaction(function() {

     // Do your SQL here

    });

    //at this point everything is OK if no exceptions is raised
}
catch( PDOException $e )
{
    //something went wrong with mysql: handle your exception
}
catch( Exception $e )
{
    //something went wrong with some other component
}

but remember that Laravel is going to handle exceptions by himself, so if you don't need to do something specific, you can assume your transaction is ok if no error is raised inside the transaction body

Upvotes: 2

Related Questions