From 10f5d4dee33456ece5ebb06fcf3988485e3ecf29 Mon Sep 17 00:00:00 2001 From: Attila Fulop <1162360+fulopattila122@users.noreply.github.com> Date: Wed, 29 May 2024 17:36:48 +0300 Subject: [PATCH] Added the feature to return linkItems directly --- Changelog.md | 6 ++++++ Query/Get.php | 20 ++++++++++++++++---- Query/WantsLinksOrGroups.php | 7 +++++++ Support/helpers.php | 9 +++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Changelog.md b/Changelog.md index c51b66e..b7a8b72 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,12 @@ ## 4.x Series +## Unreleased +##### 2024-XX-YY + +- Added the possibility to retrieve the link items directly using `linkItems()` method as `Get::the($type)->linkItems()->of($model)` +- Added the `link_items` helper (shortcut to Get::the()->linkItems() + ## 4.0.0 ##### 2024-04-25 diff --git a/Query/Get.php b/Query/Get.php index e8df290..1613bd3 100644 --- a/Query/Get.php +++ b/Query/Get.php @@ -42,9 +42,21 @@ public function of(Model $model): Collection return $groups; } - $links = collect(); - $groups->each(function ($group) use ($links, $model) { - $links->push( + $result = collect(); + if ('linkItems' === $this->wants) { + $groups->each(function ($group) use ($result, $model) { + $result->push( + ...$group + ->items + ->reject(fn ($item) => $item->linkable_id === $model->id) + ); + }); + + return $result; + } + + $groups->each(function ($group) use ($result, $model) { + $result->push( ...$group ->items ->map @@ -53,6 +65,6 @@ public function of(Model $model): Collection ); }); - return $links; + return $result; } } diff --git a/Query/WantsLinksOrGroups.php b/Query/WantsLinksOrGroups.php index dc007d1..f1dce31 100644 --- a/Query/WantsLinksOrGroups.php +++ b/Query/WantsLinksOrGroups.php @@ -31,4 +31,11 @@ public function groups(): self return $this; } + + public function linkItems(): self + { + $this->wants = 'linkItems'; + + return $this; + } } diff --git a/Support/helpers.php b/Support/helpers.php index d1ca8fc..c6a4e04 100644 --- a/Support/helpers.php +++ b/Support/helpers.php @@ -14,6 +14,15 @@ function links(string $type, string $property = null): Get } } +if (!function_exists('link_items')) { + function link_items(string $type, string $property = null): Get + { + $result = Get::the($type)->linkItems(); + + return null !== $property ? $result->basedOn($property) : $result; + } +} + if (!function_exists('link_groups')) { function link_groups(string $type, string $property = null): Get {