php 是否有一种方法可以使用laravel filament中的sortable()函数来根据优先级对帖子进行排序?

wz1wpwve  于 2022-10-30  发布在  PHP
关注(0)|答案(1)|浏览(196)

如何在laravel filament的sortable()函数中实现一个自定义比较器函数?我有三个优先级:high、medium和low,我希望将它们按high、medium和low排序,反之亦然,而不是按orderBy函数所做的升序或降序排序。我希望它位于$table列的BadgeColumn中。

  1. <?php
  2. namespace App\Filament\Resources;
  3. use App\Filament\Resources\IdeaResource\Pages;
  4. use App\Filament\Resources\IdeaResource\RelationManagers;
  5. use App\Models\Idea;
  6. use App\Models\User;
  7. use DeepCopy\Filter\Filter;
  8. use Filament\Forms;
  9. use Filament\Forms\Components\Actions\Modal\Actions\Action;
  10. use Filament\Forms\Components\Select;
  11. use Filament\Resources\Form;
  12. use Filament\Resources\Resource;
  13. use Filament\Resources\Table;
  14. use Filament\Tables;
  15. use Filament\Tables\Columns\BadgeColumn;
  16. use Illuminate\Database\Eloquent\Builder;
  17. use Illuminate\Database\Eloquent\SoftDeletingScope;
  18. use Filament\Forms\Components\DatePicker;
  19. use Filament\Tables\Columns\TextColumn;
  20. use Filament\Tables\Filters\Filter as FiltersFilter;
  21. class IdeaResource extends Resource
  22. {
  23. protected static ?string $model = Idea::class;
  24. protected static ?string $navigationIcon = 'heroicon-o-
  25. collection';
  26. public static function form(Form $form): Form
  27. {
  28. return $form
  29. ->schema([
  30. Forms\Components\Card::make()
  31. ->schema([
  32. Forms\Components\TextInput::make('title')
  33. ->label('Title')
  34. ->required(),
  35. DatePicker::make('publishing_date')
  36. ->label('Publishing Date')
  37. ->minDate('today'),
  38. // Forms\Components\MarkdownEditor::make('short_description')
  39. // ->label('Short Description')
  40. // ->columnSpan("full")
  41. // ->required(),
  42. Select::make('priority')
  43. ->options([
  44. 'high' => 'High',
  45. 'medium' => 'Medium',
  46. 'low' => 'Low'
  47. ])->required(),
  48. ])
  49. ->columns(2)
  50. ->columnSpan(['lg' => fn (?Idea $record) => $record === null ? 3 : 2]),
  51. Forms\Components\Card::make()
  52. ->schema([
  53. Forms\Components\Placeholder::make('created_at')
  54. ->label('Created at')
  55. ->content(fn (Idea $record): string => $record->created_at->diffForHumans()),
  56. Forms\Components\Placeholder::make('updated_at')
  57. ->label('Last modified at')
  58. ->content(fn (Idea $record): string => $record->updated_at->diffForHumans()),
  59. ])
  60. ->columnSpan(['lg' => 1])
  61. ->hidden(fn (?Idea $record) => $record === null),
  62. ])
  63. ->columns(3);
  64. }
  65. public static function table(Table $table): Table
  66. {
  67. return $table
  68. ->columns([
  69. Tables\Columns\TextColumn::make('title')
  70. ->label('Title')
  71. ->searchable()
  72. ->sortable(),
  73. Tables\Columns\TextColumn::make('publishing_date')
  74. ->label('Publishing Date')
  75. ->searchable()
  76. ->date()
  77. ->sortable(),
  78. BadgeColumn::make('priority')
  79. ->label('Priority')
  80. ->colors([
  81. 'primary',
  82. 'danger' => 'high',
  83. 'warning' => 'medium',
  84. 'success' => 'low',
  85. ])->sortable(query: function (Builder $query, string $direction): Builder {
  86. return $query
  87. ->orderByRaw("FIELD(priority, 'high', 'medium', 'low') ASC");
  88. // ->orderByRaw("FIELD(priority, 'low', 'medium', 'high') DESC");
  89. // ->orderByCase("
  90. // WHEN priority='high' then 1
  91. // WHEN priority='medium' then 2
  92. // WHEN priority='low' then 3
  93. // ");
  94. // ->orderBy('priority', 'asc');
  95. }),
  96. ])
  97. ->filters([
  98. //
  99. ])
  100. ->actions([
  101. Tables\Actions\Action::make('Push To Task')
  102. ->url(fn (Idea $record): string => route('idea', $record))
  103. ->icon('heroicon-s-check'),
  104. Tables\Actions\EditAction::make()
  105. ->icon('heroicon-s-pencil'),
  106. ])
  107. ->bulkActions([
  108. Tables\Actions\DeleteBulkAction::make(),
  109. ]);
  110. }
  111. public static function getRelations(): array
  112. {
  113. return [
  114. //
  115. ];
  116. }
  117. public static function getPages(): array
  118. {
  119. return [
  120. 'index' => Pages\ListIdeas::route('/'),
  121. 'create' => Pages\CreateIdea::route('/create'),
  122. 'edit' => Pages\EditIdea::route('/{record}/edit'),
  123. ];
  124. }

}
This is the priority column, needs to be sorted via high, medium and low when the user clicks the small button at the priority column.

neskvpey

neskvpey1#

我不认为你的逻辑有意义,因为排序通常用于以某种方式扫描表,我认为你需要的是基于与你的想法模型相关的类别进行过滤。
Filament有一个三元过滤器,可以在您的情况下使用它来过滤掉优先级。
三元过滤器允许您快速创建一个具有三种状态的过滤器-通常为真、假和空。
三元滤波器
使用您的方案的示例如下:

  1. TernaryFilter::make('priority')
  2. ->placeholder('High')
  3. ->trueLabel('Medium')
  4. ->falseLabel('Low')
  5. ->queries(
  6. true: fn (Builder $query) => $query->where('priority', 'medium'),
  7. false: fn (Builder $query) => $query->where('priority', 'low'),
  8. blank: fn (Builder $query) => $query->where('priority', 'high'),
  9. )

**免责声明:**此方法并没有用于其预期目的,但在您的情况下可能会很有用。干杯。

展开查看全部

相关问题