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

gaan10's avatar

I am trying to pass multiple query for execution for which i don't want data from 1st query to execute if the last query is having issues.How to achieve this.

$user = Gateway::user()->storeDoctorUser(\Input::all(), 'doctor');

$address = Gateway::address()->storeDoctorAddress(\Input::all(), $user->id);

$doctors = Gateway::doctor()->storeDoctorData(\Input::all(), $user->id,$address->id);

As stated above how to solve this issue.If somehow the doctor query is failing then also user and address are working ,which should not happen .If the last one fails all should fail.

0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104

Wrap everything in a transaction, and it will rollback if there was an exception:

DB::transaction(function () {
    $user = Gateway::user()->storeDoctorUser(\Input::all(), 'doctor');

    $address = Gateway::address()->storeDoctorAddress(\Input::all(), $user->id);

    $doctors = Gateway::doctor()->storeDoctorData(\Input::all(), $user->id,$address->id);
});

https://laravel.com/docs/5.6/database#database-transactions

1 like
gaan10's avatar

inside the controller? As the query call are going to the different pages .

Please or to participate in this conversation.