如何在laravel filament的sortable()函数中实现一个自定义比较器函数?我有三个优先级:high、medium和low,我希望将它们按high、medium和low排序,反之亦然,而不是按orderBy函数所做的升序或降序排序。我希望它位于$table列的BadgeColumn中。
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\IdeaResource\Pages;
use App\Filament\Resources\IdeaResource\RelationManagers;
use App\Models\Idea;
use App\Models\User;
use DeepCopy\Filter\Filter;
use Filament\Forms;
use Filament\Forms\Components\Actions\Modal\Actions\Action;
use Filament\Forms\Components\Select;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Tables\Columns\BadgeColumn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Forms\Components\DatePicker;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter as FiltersFilter;
class IdeaResource extends Resource
{
protected static ?string $model = Idea::class;
protected static ?string $navigationIcon = 'heroicon-o-
collection';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Card::make()
->schema([
Forms\Components\TextInput::make('title')
->label('Title')
->required(),
DatePicker::make('publishing_date')
->label('Publishing Date')
->minDate('today'),
// Forms\Components\MarkdownEditor::make('short_description')
// ->label('Short Description')
// ->columnSpan("full")
// ->required(),
Select::make('priority')
->options([
'high' => 'High',
'medium' => 'Medium',
'low' => 'Low'
])->required(),
])
->columns(2)
->columnSpan(['lg' => fn (?Idea $record) => $record === null ? 3 : 2]),
Forms\Components\Card::make()
->schema([
Forms\Components\Placeholder::make('created_at')
->label('Created at')
->content(fn (Idea $record): string => $record->created_at->diffForHumans()),
Forms\Components\Placeholder::make('updated_at')
->label('Last modified at')
->content(fn (Idea $record): string => $record->updated_at->diffForHumans()),
])
->columnSpan(['lg' => 1])
->hidden(fn (?Idea $record) => $record === null),
])
->columns(3);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('title')
->label('Title')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('publishing_date')
->label('Publishing Date')
->searchable()
->date()
->sortable(),
BadgeColumn::make('priority')
->label('Priority')
->colors([
'primary',
'danger' => 'high',
'warning' => 'medium',
'success' => 'low',
])->sortable(query: function (Builder $query, string $direction): Builder {
return $query
->orderByRaw("FIELD(priority, 'high', 'medium', 'low') ASC");
// ->orderByRaw("FIELD(priority, 'low', 'medium', 'high') DESC");
// ->orderByCase("
// WHEN priority='high' then 1
// WHEN priority='medium' then 2
// WHEN priority='low' then 3
// ");
// ->orderBy('priority', 'asc');
}),
])
->filters([
//
])
->actions([
Tables\Actions\Action::make('Push To Task')
->url(fn (Idea $record): string => route('idea', $record))
->icon('heroicon-s-check'),
Tables\Actions\EditAction::make()
->icon('heroicon-s-pencil'),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListIdeas::route('/'),
'create' => Pages\CreateIdea::route('/create'),
'edit' => Pages\EditIdea::route('/{record}/edit'),
];
}
1条答案
按热度按时间neskvpey1#
我不认为你的逻辑有意义,因为排序通常用于以某种方式扫描表,我认为你需要的是基于与你的想法模型相关的类别进行过滤。
Filament有一个三元过滤器,可以在您的情况下使用它来过滤掉优先级。
三元过滤器允许您快速创建一个具有三种状态的过滤器-通常为真、假和空。
三元滤波器
使用您的方案的示例如下:
**免责声明:**此方法并没有用于其预期目的,但在您的情况下可能会很有用。干杯。