@@ -73,7 +73,7 @@ abstract class Action implements Arrayable, Form, JsonSerializable
7373 /**
7474 * Handle the action.
7575 */
76- abstract public function handle (Request $ request , Collection $ models ): void ;
76+ abstract public function handle (Request $ request , Collection $ models ): mixed ;
7777
7878 /**
7979 * Get the key.
@@ -200,51 +200,58 @@ public function isStandalone(): bool
200200 /**
201201 * Handle the request.
202202 */
203- public function handleFormRequest (Request $ request , Model $ model ): void
203+ public function handleFormRequest (Request $ request , Model $ model ): Response
204204 {
205205 $ this ->validateFormRequest ($ request , $ model );
206206
207- $ models = match (true ) {
208- $ this ->isStandalone () => new Collection ([$ model ]),
209- $ request ->boolean ('all ' ) => $ this ->resolveQuery ($ request )->get (),
210- default => $ this ->resolveQuery ($ request )->findMany ($ request ->input ('models ' , [])),
211- };
212-
213- $ this ->handle ($ request , $ models );
214-
215- if (in_array (HasRootEvents::class, class_uses_recursive ($ model ))) {
216- $ models ->each (static function (Model $ model ) use ($ request ): void {
217- $ model ->recordRootEvent (
218- Str::of (static ::class)->classBasename ()->headline ()->value (),
219- $ request ->user ()
220- );
207+ try {
208+ return DB ::transaction (function () use ($ request , $ model ): Response {
209+ $ models = match (true ) {
210+ $ this ->isStandalone () => new Collection ([$ model ]),
211+ $ request ->boolean ('all ' ) => $ this ->resolveQuery ($ request )->get (),
212+ default => $ this ->resolveQuery ($ request )->findMany ($ request ->input ('models ' , [])),
213+ };
214+
215+ $ result = $ this ->handle ($ request , $ models );
216+
217+ if (in_array (HasRootEvents::class, class_uses_recursive ($ model ))) {
218+ $ models ->each (static function (Model $ model ) use ($ request ): void {
219+ $ model ->recordRootEvent (
220+ Str::of (static ::class)->classBasename ()->headline ()->value (),
221+ $ request ->user ()
222+ );
223+ });
224+ }
225+
226+ return $ this ->formResponse ($ request , $ model , $ result );
221227 });
228+ } catch (Throwable $ exception ) {
229+ report ($ exception );
230+
231+ throw new SaveFormDataException ($ exception ->getMessage ());
222232 }
223233 }
224234
235+ /**
236+ * Make a form response.
237+ */
238+ public function formResponse (Request $ request , Model $ model , mixed $ result ): Response
239+ {
240+ return Redirect::back ()->with (
241+ sprintf ('alerts.action-%s ' , $ this ->getKey ()),
242+ Alert::info (__ (':action was successful! ' , ['action ' => $ this ->getName ()]))
243+ );
244+ }
245+
225246 /**
226247 * Perform the action.
227248 */
228249 public function perform (Request $ request ): Response
229250 {
230- try {
231- DB ::beginTransaction ();
232-
233- $ this ->handleFormRequest ($ request , $ this ->resolveQuery ($ request )->getModel ());
234-
235- DB ::commit ();
236-
237- return Redirect::back ()->with (
238- sprintf ('alerts.action-%s ' , $ this ->getKey ()),
239- Alert::info (__ (':action was successful! ' , ['action ' => $ this ->getName ()]))
240- );
241- } catch (Throwable $ exception ) {
242- report ($ exception );
243-
244- DB ::rollBack ();
245-
246- throw new SaveFormDataException ($ exception ->getMessage ());
247- }
251+ return $ this ->handleFormRequest (
252+ $ request ,
253+ $ this ->resolveQuery ($ request )->getModel ()
254+ );
248255 }
249256
250257 /**
0 commit comments