In a foreach loop, say I wanted to do the following
foreach($items as $item)
{
// charge a user for purchasing an item - IS BEING DONE THROUGH AN API CALL WITH STRIPE
// ie: \Stripe\Charge::create(...);
// save some info to a database
// using Laravel for this project so this is within a DB::transaction(...);
// send a notification email to the buyer
// Emails are being queued to avoid slowing down the request
// ie: Mail::queue(...);
}
I would like an all or nothing situation to occur. For example, if an error occurs during the iteration, then NONE of the items are charged, no information is saved to the database, and the buyer does not get a notification email. However, if the foreach loop successfully iterates over each item, then charge the user, save info to database, and send notification email. How do I do this?