From d23397ee19ea9335b7d778f6d703dcefaa640292 Mon Sep 17 00:00:00 2001 From: Alexis Gousseau Date: Mon, 15 Dec 2025 16:35:46 +0100 Subject: [PATCH] fix: :bug: set default access URL if model method is missing --- .../Controllers/Back/InputsController.php | 22 +++++++++++++++++-- app/Models/News.php | 15 ------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/app/Http/Controllers/Back/InputsController.php b/app/Http/Controllers/Back/InputsController.php index 4ed3a80e..5a114945 100644 --- a/app/Http/Controllers/Back/InputsController.php +++ b/app/Http/Controllers/Back/InputsController.php @@ -62,7 +62,7 @@ class InputsController extends Controller ->when(!empty($validated['exclude']), function ($collection) use ($validated) { return $collection->reject(fn($item) => $item->id == $validated['exclude']); }) - ->when(!empty($validated['search']), function ($collection) use ($validated) { + ->when(!empty($validated['search']), function ($collection) use ($validated, $field) { return $collection->filter(fn($item) => is_string($value = data_get($item, $field)) && Str::contains(Str::lower($value), Str::lower($validated['search']))); }) @@ -100,7 +100,9 @@ class InputsController extends Controller return response()->json(['error' => 'Invalid model class'], 404); } - $url = $modelClass::getAccessModel($validated['selectedEntry']); + $url = (method_exists($modelClass, 'getAccessModel')) + ? $modelClass::getAccessModel($validated['selectedEntry']) + : static::getAccessModel($validated['selectedEntry'], $validated['modelName']); $token = Crypt::encryptString(json_encode([ 'modelName' => $validated['modelName'], @@ -113,6 +115,22 @@ class InputsController extends Controller ]); } + /** + * Get the url to access a specific resource. + * + * @param integer $modelId Id of the target model. + * @param integer $modelName Name of the target model. + * @return string + */ + private function getAccessModel(int $modelId, string $modelName): string + { + $targetModel = $this->getClassFromTableName($modelName)::getModelsCached()->firstWhere('id', $modelId); + if (!$targetModel) { + return null; + } + return route("front.$modelName.show", $targetModel->slug); + } + /** * Retrieve some data from specific token. * diff --git a/app/Models/News.php b/app/Models/News.php index c50e6975..f896bd8d 100644 --- a/app/Models/News.php +++ b/app/Models/News.php @@ -289,21 +289,6 @@ class News extends Model implements TranslatableModel TranslationHelper::validateFallbackTranslations($model); } - /** - * Get the url to access a specific resource. - * - * @param integer $id Id field of the target model. - * @return string - */ - public static function getAccessModel(int $id): string - { - $targetModel = self::getModelsCached()->firstWhere('id', $id); - if (!$targetModel) { - return null; - } - return route("front.news.show", $targetModel->slug); - } - // * RELATIONSHIPS /**