diff --git a/app/Http/Controllers/Dashboard/Product/Controller.php b/app/Http/Controllers/Dashboard/Product/Controller.php index 466e815..3c539c2 100755 --- a/app/Http/Controllers/Dashboard/Product/Controller.php +++ b/app/Http/Controllers/Dashboard/Product/Controller.php @@ -154,6 +154,12 @@ class Controller extends ExController { $category = Category::find($id); + if (empty($category)) { + return [ + 'status' => false, + 'characteristics' => [] + ]; + } if (!empty($category->characteristics) && count($category->characteristics) > 0) { $characteristics = $category->characteristics; diff --git a/app/Http/Requests/Dashboard/Product/Store.php b/app/Http/Requests/Dashboard/Product/Store.php index d7a9fbf..5ea3fdc 100755 --- a/app/Http/Requests/Dashboard/Product/Store.php +++ b/app/Http/Requests/Dashboard/Product/Store.php @@ -38,7 +38,7 @@ class Store extends FormRequest 'brand_id' => 'required', 'measurement_id' => 'nullable|exists:measurements,id', - 'category_id' => 'required', + 'category_id' => 'required|integer|exists:categories,id', 'colors' => 'array|required', 'colors.*.color_id' => 'nullable', 'colors.*.sizes' => 'nullable|array', diff --git a/app/Http/Requests/Dashboard/Product/Update.php b/app/Http/Requests/Dashboard/Product/Update.php index c30595e..a5d354b 100755 --- a/app/Http/Requests/Dashboard/Product/Update.php +++ b/app/Http/Requests/Dashboard/Product/Update.php @@ -28,7 +28,7 @@ class Update extends FormRequest 'price' => 'required|numeric', 'price_discount' => 'nullable', 'brand_id' => 'required', - 'category_id' => 'required', + 'category_id' => 'required|integer|exists:categories,id', 'popular' => 'nullable', "calc" => [], 'leader_of_sales' => 'nullable', diff --git a/app/Jobs/Dashboard/Product/Update.php b/app/Jobs/Dashboard/Product/Update.php index b632de9..bbbc9c0 100755 --- a/app/Jobs/Dashboard/Product/Update.php +++ b/app/Jobs/Dashboard/Product/Update.php @@ -58,13 +58,6 @@ class Update private function syncCategories() { - $cats = $this->product->categories()->get(); - - $cats = array_map(function ($cat) { - return $cat['id']; - }, $cats->toArray()); - - $this->product->categories()->detach($cats); - $this->product->categories()->attach([$this->request->getCategoryID()]); + $this->product->categories()->sync([$this->request->getCategoryID()]); } } diff --git a/resources/js/components/ProductEdit.vue b/resources/js/components/ProductEdit.vue index 59ae94a..da1fc65 100755 --- a/resources/js/components/ProductEdit.vue +++ b/resources/js/components/ProductEdit.vue @@ -1033,7 +1033,9 @@ export default { watch: { "products.category_id": function (newVal) { - this.getCharacteristics(newVal); + if (newVal) { + this.getCharacteristics(newVal); + } }, "category.first": function (newVal) { @@ -1050,8 +1052,8 @@ export default { this.DetectCategoryTwo(); } else { if (newVal.id) { - this.product.category_id = newVal.id; - this.getCharacteristics(newVal); + this.products.category_id = newVal.id; + this.getCharacteristics(newVal.id); } } } @@ -1178,7 +1180,7 @@ export default { this.category.two_view = true; } else { this.getCharacteristics(this.category.first.id); - this.product.category_id = this.category.first.id; + this.products.category_id = this.category.first.id; } }, @@ -1197,6 +1199,13 @@ export default { }, async getCharacteristics(id) { + id = id && id.id ? id.id : id; + + if (!id) { + this.characteristics = []; + return; + } + const { data } = await axios.get( "/dashboard/products/characteristics/" + id );