Saturday, March 15, 2025

Sample laravel db transaction

 DB::beginTransaction();

try {

    $exam = Exam::createForSubject($validated['subject_id'], $organization, $validated['name'], $validated['code']);

    

    $exam->update($validated);

    

    DB::commit(); // ✅ Commit only if everything succeeds

} catch (\Exception $e) {

    DB::rollBack(); // ❌ Rollback on error

    throw $e; // Optionally rethrow the error

}


Sample laravel db transaction