| 257 | 4. Rollback при грешка |
| 258 | {{{ |
| 259 | DB::transaction(function () use ($request) { |
| 260 | try { |
| 261 | DB::table('activity_reservation')->insert([ |
| 262 | 'id_user' => auth()->id(), |
| 263 | 'id_activity' => $request->activity_id, |
| 264 | 'reservation_date' => now() |
| 265 | ]); |
| 266 | |
| 267 | if ($request->activity_id == 999) { |
| 268 | throw new Exception('Непозната активност'); |
| 269 | } |
| 270 | |
| 271 | DB::table('activity')->where('id_activity', $request->activity_id)->decrement('amount', 1); |
| 272 | } catch (Exception $e) { |
| 273 | echo 'Трансакцијата не успеа: ', $e->getMessage(); |
| 274 | throw $e; |
| 275 | } |
| 276 | }); |
| 277 | }}} |
| 278 | |