*/ protected array $allowedColumns; /** * Sortable column rule */ public function __construct(array $allowedColumns) { $this->allowedColumns = $allowedColumns; } /** * Run the validation rule. * * @param Closure(string): PotentiallyTranslatedString $fail */ public function validate(string $attribute, mixed $value, Closure $fail): void { // Format should be "coloumn-sort", example: 'created_at-descending' $request_data = explode('-', $value); if (count($request_data) !== 2) { $fail(__('Format should be: "coloumn-sort"')); return; } [$column, $sort] = $request_data; if (! in_array($column, $this->allowedColumns)) { $fail(__("Sorting by this {$column} is not allowed")); return; } if (! in_array($sort, ['descending', 'ascending'])) { $fail(__('You can only sort by ascending or descending!')); return; } } }