The usage of transaction in laravel

is it okay to write transactions like this, and do you need custom exceptions?

DB::beginTransaction();
try{
    // 
    $adminRoleObj = new AdminRole;
    $result = $adminRoleObj->deleteAdminRole($admin->id);

    // 
    $result = $admin->delete();

    DB::commit();
}catch(\Exception $e){
    $result = false;
    Log::error("admin:delete ".$e->getMessage());
    DB::rollBack();
}

clipboard.png

May.05,2021

Congratulations, the writing is correct, there is no doubt.
there is no need to customize exceptions in your business of directly manipulating the library. Database execution errors or exceptions will throw exceptions themselves.

Menu