45 lines
1.3 KiB
PHP
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',
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|