Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

richbreton's avatar

how to know if DB::transaction failed

I have some code that runs every night as a transaction:

DB::transaction(function() {

    $vitelity = new Vitelity;
    $calls = $vitelity->getAllCalls();

    DB::table('calls')->insert($calls);
});

I know that DB::transaction will automatically rollback if it encounters any errors, but how do I catch that it has rolled back and find out what the error is. Ill probably use mailgun to send the info to me when I can figure out how to get the info when it happens.

0 likes
7 replies
richbreton's avatar

wow I appreciate the link a lot but its a lot of info and I think what I want is much simpler I just want to know if the transaction automatically got rolled back in the above code example. i.e. what to test for

richbreton's avatar

maybe something like:


DB::transaction(function() { $vitelity = new Vitelity; $calls = $vitelity->getAllCalls(); try { DB::table('calls')->insert($calls); } catch (\PDOException $e) { //send mail with subject "db import failed" and body of $e->getMessage() } });

that seems like it could work right?

RachidLaasri's avatar

Yes, Putting your code inside a try and catching any exception will works just fine.

Please or to participate in this conversation.