【发布时间】:2018-03-02 23:41:33
【问题描述】:
我想直接将数据存储在 5 个表中。我成功地将数据存储/保存在 4 个表中,但在最后一个表中出现错误。如何回滚前 4 个表中的记录?
这是我的控制器
$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>';
}
【问题讨论】:
标签: php mysql database laravel-5