Refactor Product and Category Tables for Enhanced Localization: Updated the ProductCategoriesTable and ProductsTable to utilize the getStateUsing method for retrieving translated titles, improving localization support. Enhanced the search functionality to filter by translations, ensuring consistent display of titles across different languages. Additionally, modified the menu-item Blade component to include featured products, improving the layout and user experience.

This commit is contained in:
Ümit Tunç
2026-01-01 11:13:45 +03:00
parent 12441517f6
commit e8cdba4fd3
3 changed files with 52 additions and 30 deletions
@@ -18,14 +18,19 @@ class ProductCategoriesTable
->columns([
TextColumn::make('title')
->label(__('product_categories.title'))
->formatStateUsing(fn ($state, $record) => $record->translate('title'))
->searchable()
->sortable(),
->getStateUsing(fn ($record) => $record->translate('title'))
->searchable(query: function ($query, $search) {
return $query->whereHas('translations', function ($q) use ($search) {
$q->where('field_name', 'title')
->where('field_value', 'like', "%{$search}%")
->where('language_code', app()->getLocale());
});
}),
TextColumn::make('slug')
->label(__('product_categories.slug'))
->searchable(),
TextColumn::make('parent.title')
->formatStateUsing(fn ($state, $record) => $record->parent?->translate('title'))
->getStateUsing(fn ($record) => $record->parent?->translate('title'))
->label(__('product_categories.parent')),
TextColumn::make('sort_order')
->label(__('product_categories.sort_order'))
@@ -18,12 +18,18 @@ class ProductsTable
return $table
->columns([
ImageColumn::make('hero_image')
->label(__('products.hero_image')),
->label(__('products.hero_image'))
->disk('public'),
TextColumn::make('title')
->label(__('products.title'))
->formatStateUsing(fn ($state, $record) => $record->translate('title'))
->searchable()
->sortable(),
->getStateUsing(fn ($record) => $record->translate('title'))
->searchable(query: function ($query, $search) {
return $query->whereHas('translations', function ($q) use ($search) {
$q->where('field_name', 'title')
->where('field_value', 'like', "%{$search}%")
->where('language_code', app()->getLocale());
});
}),
TextColumn::make('type')
->label(__('products.type'))
->badge()
@@ -38,7 +44,7 @@ class ProductsTable
default => $state,
}),
TextColumn::make('category.title')
->formatStateUsing(fn ($state, $record) => $record->category?->translate('title'))
->getStateUsing(fn ($record) => $record->category?->translate('title'))
->label(__('products.category')),
ToggleColumn::make('is_active')
->label(__('products.is_active')),
@@ -6,9 +6,11 @@
$categories = [];
$services = [];
$featuredProducts = [];
if($isMegaMenu) {
$categories = \App\Models\ProductCategory::where('is_active', true)->orderBy('sort_order')->get();
$services = \App\Models\Product::where('is_active', true)->orderBy('sort_order')->limit(10)->get();
$services = \App\Models\Product::where('is_active', true)->where('type', 'service')->orderBy('sort_order')->limit(10)->get();
$featuredProducts = \App\Models\Product::where('is_active', true)->where('type', '!=', 'service')->inRandomOrder()->limit(6)->get();
}
@endphp
@@ -18,37 +20,46 @@
<ul class="dropdown-menu mega-menu">
<li class="mega-menu-content">
<div class="flex flex-wrap mx-0 xl:mx-[-7.5px] lg:mx-[-7.5px]">
<div class="xl:w-4/12 lg:w-4/12 w-full flex-[0_0_auto] max-w-full">
<div class="xl:w-3/12 lg:w-3/12 w-full flex-[0_0_auto] max-w-full">
<h6 class="dropdown-header !text-[#e31e24]">{{ __('Kategoriler') }}</h6>
<ul class="pl-0 list-none xl:columns-2 lg:columns-2 xl:pb-1 lg:pb-1">
<ul class="pl-0 list-none xl:pb-1 lg:pb-1">
@foreach($categories as $category)
<li class="xl:inline-block xl:w-full lg:inline-block lg:w-full">
<li class="w-full">
<a class='dropdown-item hover:!text-[#e31e24]' href='#'>{{ $category->translate('title') }}</a>
</li>
@endforeach
</ul>
</div>
<!--/column -->
<div class="xl:w-8/12 lg:w-8/12 w-full flex-[0_0_auto] max-w-full xl:border-l-[rgba(164,174,198,0.2)] xl:border-l xl:border-solid lg:border-l-[rgba(164,174,198,0.2)] lg:border-l lg:border-solid xl:pl-[15px] lg:pl-[15px]">
<h6 class="dropdown-header !text-[#e31e24] !ml-[10px]">{{ __('Öne Çıkan Ürünler') }}</h6>
<ul class="pl-0 list-none flex flex-wrap -mx-[10px]">
<h6 class="dropdown-header !text-[#e31e24] mt-5">{{ __('Hizmetler') }}</h6>
<ul class="pl-0 list-none xl:pb-1 lg:pb-1">
@foreach($services as $service)
<li class="xl:w-4/12 lg:w-4/12 w-full flex-[0_0_auto] xl:px-[10px] xl:mt-[10px] lg:px-[10px] lg:mt-[10px]">
<a class="dropdown-item group !bg-transparent !p-0 text-center" href="{{ route('products.show', $service->slug) }}">
<figure class="!rounded-[.4rem] lift hidden xl:block lg:block !mb-2 shadow-sm border border-gray-100 relative overflow-hidden aspect-[4/3]">
@if($service->hero_image)
<img class="!rounded-[.4rem] w-full h-full object-cover transition-all duration-300 group-hover:scale-105" src="{{ \Storage::url($service->hero_image) }}" alt="{{ $service->translate('title') }}">
@else
<img class="!rounded-[.4rem] w-full h-full object-cover" src="https://placehold.co/600x400?text={{ urlencode($service->translate('title')) }}" alt="{{ $service->translate('title') }}">
@endif
</figure>
<span class="xl:!hidden lg:!hidden">{{ $service->translate('title') }}</span>
<span class="hidden xl:block lg:block text-xs font-bold text-gray-800 group-hover:text-[#e31e24]">{{ $service->translate('title') }}</span>
</a>
<li class="w-full">
<a class='dropdown-item hover:!text-[#e31e24]' href="{{ route('products.show', $service->slug) }}">{{ $service->translate('title') }}</a>
</li>
@endforeach
</ul>
</div>
<div class="xl:w-9/12 lg:w-9/12 w-full flex-[0_0_auto] max-w-full xl:border-l-[rgba(164,174,198,0.2)] xl:border-l xl:border-solid lg:border-l-[rgba(164,174,198,0.2)] lg:border-l lg:border-solid xl:pl-[15px] lg:pl-[15px]">
<h6 class="dropdown-header !text-[#e31e24] !ml-[10px]">{{ __('Öne Çıkan Ürünler') }}</h6>
<div class="flex flex-wrap p-2 gap-4">
@foreach($featuredProducts as $item)
<div class="flex-none w-[185px]">
<a class="dropdown-item group !bg-transparent !p-0 text-center block" href="{{ route('products.show', $item->slug) }}">
<figure class="!rounded-[.4rem] overflow-hidden w-[185px] h-[135px] !mb-2 shadow-sm border border-gray-100 mx-auto">
@if($item->hero_image)
<img class="!rounded-[.4rem] w-full h-full object-cover transition-all duration-300 group-hover:scale-105" src="{{ \Storage::url($item->hero_image) }}" alt="{{ $item->translate('title') }}" width="185" height="135">
@else
<img class="!rounded-[.4rem] w-full h-full object-cover" src="https://placehold.co/185x135?text={{ urlencode($item->translate('title')) }}" alt="{{ $item->translate('title') }}" width="185" height="135">
@endif
</figure>
<span class="xl:!hidden lg:!hidden">{{ $item->translate('title') }}</span>
<span class="hidden xl:block lg:block text-xs font-bold text-gray-800 group-hover:text-[#e31e24] truncate w-full text-center">{{ $item->translate('title') }}</span>
</a>
</div>
@endforeach
</div>
</div>
<!--/column -->
</div>
<!--/.row -->