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

uchihaabhi's avatar

Test transactions in Laravel

I have been trying to implement transaction in laravel. But I guess it isn't working. When I change the database field/column name (throwing a force error) the earlier tables gets inserted instead of rolling back. What is happening here??

try {

                DB::beginTransaction();
                   
                $drugInventory = DrugInventory::create([
                    'service_pro_id' => $service_pro_id,
                    'brand_name' => $brand_name,
                    'manufacturer' => $manufacturer,
                ]);

                $drug_entities = new DrugEntity;
                $drug_entities->tpe = $type;    //here I have written tpe instead of type

                $drug_entities->save();


                

                DB::commit();


            } catch( Exception $e ) {
                DB::rollback();
                echo $e->getMessage();
            }
0 likes
3 replies
tomopongrac's avatar

Try put DB::beginTransaction() before try block

DB::beginTransaction();

try {
                   
                $drugInventory = DrugInventory::create([
                    'service_pro_id' => $service_pro_id,
                    'brand_name' => $brand_name,
                    'manufacturer' => $manufacturer,
                ]);

                $drug_entities = new DrugEntity;
                $drug_entities->tpe = $type;    //here I have written tpe instead of type

                $drug_entities->save();


                

                DB::commit();


            } catch( Exception $e ) {
                DB::rollback();
                echo $e->getMessage();
            }
uchihaabhi's avatar
uchihaabhi
OP
Best Answer
Level 1

Solved. The logic was correct except for the part that my database engine was MYISAM, changed it to InnoDB and now it's working fine

1 like

Please or to participate in this conversation.