CategoryResource.php
TLDR
This file is a resource class for managing categories in an e-commerce system. It provides methods for creating forms, tables, and managing relations.
Methods
There are no methods in this file.
Classes
Class CategoryResource
This class extends the Resource
class and represents a resource for managing categories in an e-commerce system. It provides methods for creating forms, tables, and managing relations.
The class has the following properties:
-
$model
: The model class representing the category (app\Filament\Models\Shop\Category). -
$slug
: The URI slug for accessing the category resource ('shop/categories'). -
$recordTitleAttribute
: The attribute used as the record title (name). -
$navigationGroup
: The navigation group for the category resource ('Shop'). -
$navigationIcon
: The navigation icon for the category resource ('heroicon-o-tag'). -
$navigationParentItem
: The parent navigation item for the category resource ('Products'). -
$navigationSort
: The sorting order of the category resource in the navigation (3).
The class has the following methods:
-
form(Form $form): Form
: Returns a form schema for creating/editing a category. The form includes fields for name, slug, parent category, visibility, and description. -
table(Table $table): Table
: Returns a table schema for displaying a list of categories. The table includes columns for name, parent category, visibility, and updated date. -
getRelations(): array
: Returns an array of relation manager classes for the category resource. -
getPages(): array
: Returns an array of page classes for the category resource.
<?php
namespace App\Filament\Resources\Shop;
use App\Filament\Resources\Shop\CategoryResource\Pages;
use App\Filament\Resources\Shop\CategoryResource\RelationManagers;
use App\Models\Shop\Category;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
class CategoryResource extends Resource
{
protected static ?string $model = Category::class;
protected static ?string $slug = 'shop/categories';
protected static ?string $recordTitleAttribute = 'name';
protected static ?string $navigationGroup = 'Shop';
protected static ?string $navigationIcon = 'heroicon-o-tag';
protected static ?string $navigationParentItem = 'Products';
protected static ?int $navigationSort = 3;
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Section::make()
->schema([
Forms\Components\Grid::make()
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255)
->live(onBlur: true)
->afterStateUpdated(fn (string $operation, $state, Forms\Set $set) => $operation === 'create' ? $set('slug', Str::slug($state)) : null),
Forms\Components\TextInput::make('slug')
->disabled()
->dehydrated()
->required()
->maxLength(255)
->unique(Category::class, 'slug', ignoreRecord: true),
]),
Forms\Components\Select::make('parent_id')
->label('Parent')
->relationship('parent', 'name', fn (Builder $query) => $query->where('parent_id', null))
->searchable()
->placeholder('Select parent category'),
Forms\Components\Toggle::make('is_visible')
->label('Visible to customers.')
->default(true),
Forms\Components\MarkdownEditor::make('description')
->label('Description'),
])
->columnSpan(['lg' => fn (?Category $record) => $record === null ? 3 : 2]),
Forms\Components\Section::make()
->schema([
Forms\Components\Placeholder::make('created_at')
->label('Created at')
->content(fn (Category $record): ?string => $record->created_at?->diffForHumans()),
Forms\Components\Placeholder::make('updated_at')
->label('Last modified at')
->content(fn (Category $record): ?string => $record->updated_at?->diffForHumans()),
])
->columnSpan(['lg' => 1])
->hidden(fn (?Category $record) => $record === null),
])
->columns(3);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->label('Name')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('parent.name')
->label('Parent')
->searchable()
->sortable(),
Tables\Columns\IconColumn::make('is_visible')
->label('Visibility')
->sortable(),
Tables\Columns\TextColumn::make('updated_at')
->label('Updated Date')
->date()
->sortable(),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->groupedBulkActions([
Tables\Actions\DeleteBulkAction::make()
->action(function () {
Notification::make()
->title('Now, now, don\'t be cheeky, leave some records for others to play with!')
->warning()
->send();
}),
]);
}
public static function getRelations(): array
{
return [
RelationManagers\ProductsRelationManager::class,
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListCategories::route('/'),
'create' => Pages\CreateCategory::route('/create'),
'edit' => Pages\EditCategory::route('/{record}/edit'),
];
}
}