I want to store data in 5 tables directly. I store/save data in 4 tables with success, but I get an error in the last table. How do I rollback the records in the first 4 tables?
This is my controller
$booking = new Book();
$booking->date = Carbon::now();
$booking->unique_number = mt_rand(1, 999);
$booking->save();
$bookingDetail = new BookDetail();
$bookingDetail->book()->associate($booking);
$bookingDetail->qty = $request->qty;
$bookingDetail->event_id = $id;
$bookingDetail->price = $event->price;
$bookingDetail->subtotal = $bookingDetail->qty * $bookingDetail->price;
$bookingDetail->save();
$booking->total = $bookingDetail->subtotal + $booking->unique_number;
$booking->save();
$bookingConfirmation = new BookConfirmation();
$bookingConfirmation->book()->associate($booking);
$bookingConfirmation->save();
$bookingCompletion = new BookCompletion();
$bookingCompletion->book()->associate($booking);
$bookingCompletion->save();
$participants = explode(', ', $request->participant_name);
if(count($participants) === $bookingDetail->qty){
foreach ($participants as $participant) {
$bookingParticipant = new BookParticipant();
$bookingParticipant->participant_name = $participant;
$bookingParticipant->save();
}
}else{
echo '<script language="javascript">alert("Jumlah orang tidak sesuai")</script>';
}