@php use Illuminate\Support\Facades\Cache; $categoriesList = Cache::remember('mega_menu.categories.v1', 600, function () { return \App\Models\Category::forEcommerce()->where('status', 1)->get(['id', 'name', 'slug', 'parent_id']); }); $catMap = $categoriesList->pluck('slug', 'name')->toArray(); $getCatLink = function($name) use ($catMap) { if (isset($catMap[$name])) { return route('category', ['slug' => $catMap[$name]]); } // Fallback to slugified name if not found in DB return route('category', ['slug' => \Illuminate\Support\Str::slug($name)]); }; @endphp