Files
sam-api/app/Http/Requests/Traits/HasPagination.php
kent 97f22f9b98 refactor(pagination): size 초과 시 오류 대신 자동 조정으로 변경
- config/pagination.php 추가 (기본값 중앙 관리)
- HasPagination Trait 추가 (prepareForValidation에서 자동 조정)
- 22개 IndexRequest에 Trait 적용, max 규칙 제거
- 특수 케이스: Employee($maxSize=500), Audit($maxSize=200)

size/per_page가 최대값 초과 시 422 오류 대신 최대값으로 자동 조정되어
리스트가 빈 화면으로 표시되는 문제 해결

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-13 19:45:44 +09:00

45 lines
1.3 KiB
PHP

<?php
namespace App\Http\Requests\Traits;
/**
* 페이지네이션 공통 처리 Trait
*
* size 또는 per_page 파라미터가 maxSize를 초과하면 자동으로 maxSize로 조정합니다.
* 오류를 발생시키지 않고 최대값으로 제한하여 빈 리스트 문제를 방지합니다.
*
* @property int|null $maxSize 최대 페이지 사이즈 (기본값: config('pagination.max_size'))
*/
trait HasPagination
{
/**
* 검증 전 size/per_page 값을 maxSize 이하로 조정
*/
protected function prepareForValidation(): void
{
$maxSize = $this->maxSize ?? config('pagination.max_size', 100);
// size 파라미터 처리
if ($this->has('size') && $this->input('size') > $maxSize) {
$this->merge(['size' => $maxSize]);
}
// per_page 파라미터 처리
if ($this->has('per_page') && $this->input('per_page') > $maxSize) {
$this->merge(['per_page' => $maxSize]);
}
}
/**
* 페이지네이션 기본 규칙 반환
*/
protected function paginationRules(): array
{
return [
'page' => 'nullable|integer|min:1',
'size' => 'nullable|integer|min:1',
'per_page' => 'nullable|integer|min:1',
];
}
}