all(), ['grid' => 'numeric']); if ($validate->fails()) { return false; } return \request()->grid; } public function __construct( public GroupRepositoryInterface $groupRepository, public LogRepositoryInterface $logRepository ) { parent::__construct(); } //validate Group ID /** * Add new group */ public function addGroupInformation() { $title = 'Add New Product Group'; return view('admin.product.group', compact('title')); } /** * Store information group */ public function storeGroupInformation(Request $request) { $request->validate([ 'name' => 'required', 'type_id' => 'required', 'status' => 'required', ]); try { DB::beginTransaction(); $this->groupRepository->create($request->only(['name', 'type_id', 'status'])); // Clear all product-related caches after group creation $this->clearGroupCaches($request->type_id); DB::commit(); } catch (\Exception $error) { DB::rollBack(); return back()->withErrors($error->getMessage()); } return redirect()->route('product.index')->with('success', 'Add New Product Group Success'); } /** * View information group */ public function getGroupInformation(Request $request) { $group_id = $this->validateGrid(); if (!$group_id) { return redirect()->route('product.index')->withErrors('No information data found'); } $group = Group::find($group_id); if (!$group) { return redirect()->route('product.index')->withErrors('No information data found'); } $type = Type::select('id', 'name')->get(); $title = 'Action Group'; return view("admin.product.group", compact('title', 'type', 'group', 'group_id')); } /** * Save information group */ public function updateGroupInformation(GroupRequest $request) { $data = $request->validated(); $group = Group::find($data['grid']); if (!$group) { return redirect()->route('product.index')->withErrors('No information data found'); } try { DB::beginTransaction(); Group::where('id', $group->id)->update($request->except(['_token', 'grid', '_method'])); // Clear all product-related caches after group update $this->clearGroupCaches($group->type_id); Log::create([ 'agent' => $request->userAgent(), 'client_ip' => $request->getClientIp(), 'username' => $request->user('admins')->username, 'description' => $request->user('admins')->username . " modify Group Product", 'customer_id' => 0 ]); DB::commit(); } catch (\Exception $error) { DB::rollBack(); return back()->withErrors($error->getMessage()); } return redirect()->route('product.index')->with('success', 'Updated Product Group success'); } public function removeGroup(Request $request) { $request->validate([ 'group_id' => 'required' ]); $group = Group::find($request->group_id); // Clear caches before deletion $this->clearGroupCaches($group->type_id); $group->delete(); return redirect()->route('product.index')->with('success', 'Product group deleted successfully'); } /** * Clear all group and product-related caches */ private function clearGroupCaches($typeId) { // Clear general shop caches Cache::forget('shop_types_with_groups'); if ($typeId) { // Clear type-specific group cache Cache::forget("groups_type_{$typeId}"); // Clear all product caches for this type $groups = $this->groupRepository->filter(['type_id' => $typeId]); foreach ($groups as $group) { Cache::forget("products_type_{$typeId}_group_{$group->id}"); if ($group->slug) { // Clear paginated product caches for multiple pages for ($page = 1; $page <= 10; $page++) { Cache::forget("group_products_{$group->slug}_page_{$page}"); } } } } } }