currentStore($request); $search = trim((string) $request->query('q', '')); $categories = WooCategory::query() ->when($store, fn ($q) => $q->where('woo_store_id', $store->id)) ->when(! $store, fn ($q) => $q->whereRaw('1 = 0')) ->when($search !== '', fn ($q) => $q->where('name', 'like', '%'.$search.'%')) ->with('store') ->orderBy('name') ->paginate(30) ->withQueryString(); return view('woo.categories.index', compact('categories', 'search', 'store')); } public function create(Request $request): View { $store = $this->currentStoreOrFail($request); return view('woo.categories.form', [ 'category' => new WooCategory(['woo_store_id' => $store->id]), 'currentStore' => $store, 'parentCategories' => $store->categories()->orderBy('name')->get(), ]); } public function store(Request $request): RedirectResponse { $store = $this->currentStoreOrFail($request); $validated = $this->validateCategory($request, $store); $this->write->createCategory($store, $validated); return redirect()->route('woo.categories.index') ->with('success', 'Category saved.'); } public function edit(Request $request, WooCategory $category): View { $store = $this->currentStoreOrFail($request); abort_if($category->woo_store_id !== $store->id, 404); return view('woo.categories.form', [ 'category' => $category, 'currentStore' => $store, 'parentCategories' => $store->categories() ->where('external_id', '!=', $category->external_id) ->orderBy('name') ->get(), ]); } public function update(Request $request, WooCategory $category): RedirectResponse { $store = $this->currentStoreOrFail($request); abort_if($category->woo_store_id !== $store->id, 404); $validated = $this->validateCategory($request, $store); $this->write->updateCategory($category, $validated); return redirect()->route('woo.categories.index') ->with('success', 'Category updated.'); } public function destroy(Request $request, WooCategory $category): RedirectResponse { $store = $this->currentStoreOrFail($request); abort_if($category->woo_store_id !== $store->id, 404); $this->write->deleteCategory($category); return redirect()->route('woo.categories.index') ->with('success', 'Category removed.'); } public function sync(Request $request): RedirectResponse { $store = $this->currentStoreOrFail($request); $count = $this->sync->syncCategories($store); return back()->with('success', sprintf( 'Synced %d categories from %s.', $count, $store->site_name ?? $store->site_url, )); } /** @return array */ private function validateCategory(Request $request, $store): array { return array_merge($request->validate([ 'name' => ['required', 'string', 'max:255'], 'slug' => ['nullable', 'string', 'max:255'], 'description' => ['nullable', 'string', 'max:10000'], 'parent_external_id' => ['nullable', 'integer', 'min:0'], ]), [ 'woo_store_id' => $store->id, ]); } }