From 5a602cbb738d2a1be8aedf6a750d2c779148de41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=BCneyt=20=C5=9Eent=C3=BCrk?= Date: Fri, 17 May 2024 13:55:47 +0300 Subject: [PATCH] Added category colors next to options vol 2 --- app/View/Components/Form/Group/Category.php | 22 ++++++++++++++----- .../components/form/group/category.blade.php | 16 +++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/View/Components/Form/Group/Category.php b/app/View/Components/Form/Group/Category.php index 6a8473d87..45d9821b3 100644 --- a/app/View/Components/Form/Group/Category.php +++ b/app/View/Components/Form/Group/Category.php @@ -29,7 +29,7 @@ class Category extends Form $this->path = route('modals.categories.create', ['type' => $this->type]); $this->remoteAction = route('categories.index', ['search' => 'type:' . $this->type . ' enabled:1']); - $this->categories = Model::type($this->type)->enabled()->orderBy('name')->take(setting('default.select_limit'))->pluck('name', 'id'); + $this->categories = Model::type($this->type)->enabled()->orderBy('name')->take(setting('default.select_limit'))->get(); $model = $this->getParentData('model'); @@ -38,10 +38,14 @@ class Category extends Form if (! empty($category_id)) { $this->selected = $category_id; - if (! $this->categories->has($category_id)) { + $has_category = $this->categories->search(function ($category, int $key) use ($category_id) { + return $category->id === $category_id; + }); + + if (! $has_category) { $category = Model::find($category_id); - $this->categories->put($category->id, $category->name); + $this->categories->push($category); } } @@ -57,8 +61,16 @@ class Category extends Form $selected_category = Model::find($this->selected); } - if (! empty($selected_category) && ! $this->categories->has($selected_category->id)) { - $this->categories->put($selected_category->id, $selected_category->name); + if (! empty($selected_category)) { + $selected_category_id = $selected_category->id; + + $has_selected_category = $this->categories->search(function ($category, int $key) use ($selected_category_id) { + return $category->id === $selected_category_id; + }); + + if (! $has_selected_category) { + $this->categories->push($selected_category); + } } return view('components.form.group.category'); diff --git a/resources/views/components/form/group/category.blade.php b/resources/views/components/form/group/category.blade.php index c5c3108f8..4486ba65a 100644 --- a/resources/views/components/form/group/category.blade.php +++ b/resources/views/components/form/group/category.blade.php @@ -23,8 +23,8 @@ > @@ -50,8 +50,8 @@ > @@ -77,8 +77,8 @@ > @@ -101,8 +101,8 @@ >