diff --git a/composer.json b/composer.json index 78550c5..efcfcaf 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "fleetbase/core-api", - "version": "1.6.44", + "version": "1.6.45", "description": "Core Framework and Resources for Fleetbase API", "keywords": [ "fleetbase", diff --git a/src/Traits/HasApiModelBehavior.php b/src/Traits/HasApiModelBehavior.php index 31cebf9..0debae0 100644 --- a/src/Traits/HasApiModelBehavior.php +++ b/src/Traits/HasApiModelBehavior.php @@ -326,6 +326,15 @@ public function createRecordFromRequest($request, ?callable $onBefore = null, ?c return $record; } + if (is_callable($onAfter)) { + $after = $onAfter($request, $record, $input); + if ($after instanceof JsonResponse) { + return $after; + } + } + + $record->refresh(); + // PERFORMANCE OPTIMIZATION: Use load() instead of re-querying the database // This avoids an unnecessary second database query $with = $request->or(['with', 'expand'], []); @@ -339,13 +348,6 @@ public function createRecordFromRequest($request, ?callable $onBefore = null, ?c $record->loadCount($withCount); } - if (is_callable($onAfter)) { - $after = $onAfter($request, $record, $input); - if ($after instanceof JsonResponse) { - return $after; - } - } - return static::mutateModelWithRequest($request, $record); } @@ -418,6 +420,15 @@ public function updateRecordFromRequest(Request $request, $id, ?callable $onBefo return $record; } + if (is_callable($onAfter)) { + $after = $onAfter($request, $record, $input); + if ($after instanceof JsonResponse) { + return $after; + } + } + + $record->refresh(); + // PERFORMANCE OPTIMIZATION: Use load() instead of re-querying the database // This avoids an unnecessary second database query $with = $request->or(['with', 'expand'], []); @@ -431,13 +442,6 @@ public function updateRecordFromRequest(Request $request, $id, ?callable $onBefo $record->loadCount($withCount); } - if (is_callable($onAfter)) { - $after = $onAfter($request, $record, $input); - if ($after instanceof JsonResponse) { - return $after; - } - } - return static::mutateModelWithRequest($request, $record); }