Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check first all model if it can be saved or not.Then save

Check first all model if it can be saved or not.Then save.

i tried to saved data from query. i have 3 query :

$message_header->save();

$save_receive_info->save_receive_info_id = $message_header->id;
$information_receive->save();

$information_chargeline->save_receive_info_details_id = $information_receive->id;
$maklumat_chargeline->save();

so, based on this query, first it saves $message_header, then if save_receive_info, but if receive_info has an error when saving, what can I do?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I mean, I want to check whether all queries are executable or not then save. I have a problem with this because other queries are stored based on other id

please help. and sorry for my broken english.

>Solution :

You can use try and catch block with DB transactions.

use Illuminate\Support\Facades\DB;


DB::beginTransaction();
try {
    // your code logic before save

    $message_header->save();

    $save_receive_info->save_receive_info_id = $message_header->id;
    $information_receive->save();

    $information_chargeline->save_receive_info_details_id = $information_receive->id;
    $maklumat_chargeline->save();

    DB::commit();
    // all good
} catch (\Exception $e) {
    DB::rollback();
    // something went wrong
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading