| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace App\Http\Requests;
- use Illuminate\Foundation\Http\FormRequest;
- class ListRequest extends FormRequest
- {
- public function rules(): array
- {
- return [
- 'sort_by' => 'sometimes|string',
- 'sort_order' => 'sometimes|in:asc,desc',
- 'page' => 'sometimes|integer|min:1',
- 'per_page' => 'sometimes|integer|min:1|max:100',
- ];
- }
- /**
- * 自定义错误消息
- * @return string[]
- */
- public function messages(): array
- {
- return [
- 'search.max' => '搜索关键字不能超过 100 个',
- // 'filters.status.in' => '状态必须是「0:启用」或「1:禁用」',
- // 'sort_by.in' => '排序字段只能是 id、create_time 或 update_time',
- 'sort_order.in' => '排序方向必须是(asc:升序、desc:降序)',
- 'page.min' => '当前页最小数字为 1',
- 'per_page.min' => '每页数量最小数字为 1',
- ];
- }
- public function getSearch(): string
- {
- return $this->input('search') ?? '';
- }
- public function getFilters(): array
- {
- return $this->input('filters', []);
- }
- public function getSort(): array
- {
- return [
- 'sort_by' => $this->input('sort_by', 'id'),
- 'sort_order' => $this->input('sort_order', 'desc'),
- ];
- }
- public function getPerPage(): int
- {
- return $this->input('per_page', 20);
- }
- }
|