From 52bcabd2be1edcf23979e4f060b1b389507c7bfa Mon Sep 17 00:00:00 2001 From: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com> Date: Tue, 10 May 2022 15:01:13 +0100 Subject: [PATCH] Ensure resource primary key is included in response Signed-off-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com> --- Extension.php | 6 ++++++ classes/Scope.php | 26 ++++++++++++++++++++++++++ classes/ScopeFactory.php | 15 +++++++++++++++ classes/TransformerAbstract.php | 4 ++++ composer.json | 5 +++++ serializer/JsonApiSerializer.php | 16 ---------------- 6 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 classes/Scope.php create mode 100644 classes/ScopeFactory.php diff --git a/Extension.php b/Extension.php index de20fa2..8048b51 100755 --- a/Extension.php +++ b/Extension.php @@ -5,6 +5,7 @@ use Admin\Models\Customers_model; use Admin\Models\Users_model; use Dingo\Api\Auth\Auth; +use Igniter\Api\Classes\ScopeFactory; use Igniter\Flame\Database\Model; use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Contracts\Http\Kernel; @@ -12,6 +13,7 @@ use Illuminate\Support\Facades\RateLimiter; use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful; use Laravel\Sanctum\Sanctum; +use League\Fractal\Manager; use System\Classes\BaseExtension; /** @@ -193,6 +195,10 @@ public function registerApiResources() */ protected function registerResponseFactory() { + $this->app->bind(Manager::class, function () { + return new Manager(new ScopeFactory); + }); + $this->app->alias('api.response', Classes\ResponseFactory::class); $this->app->singleton('api.response', function ($app) { diff --git a/classes/Scope.php b/classes/Scope.php new file mode 100644 index 0000000..522cb86 --- /dev/null +++ b/classes/Scope.php @@ -0,0 +1,26 @@ +getKey(); + + return [$transformedData, $includedData]; + } +} diff --git a/classes/ScopeFactory.php b/classes/ScopeFactory.php new file mode 100644 index 0000000..49510d2 --- /dev/null +++ b/classes/ScopeFactory.php @@ -0,0 +1,15 @@ + array_get($data, $keyName), - ]); - - unset($data[$keyName]); - } - - return parent::item($resourceKey, $data); - } }