티스토리 뷰

https://laravel.com/docs/7.x/pagination#introduction

 

Database: Pagination - Laravel - The PHP Framework For Web Artisans

 

laravel.com

 

일단 여기 페이지 보면, 

Laravel's paginator is integrated with the query builder and Eloquent ORM and provides convenient,

Laravel's paginator is integrated with the query builder and Eloquent ORM and provides convenient,
easy-to-use pagination of database results out of the box.

랜다. 짧은 영어로 해석해보면

라라벨 페이지네이터는, 통합되어 있다.

프로바이더 컨비니언(기본제공해주는? 헬퍼파일같은?)이랑 엘로퀀트 오알엠이랑 쿼리빌더랑 함께.

쉽게 데이터베이스 결과를 페이지네이션을 사용할 수 있다.

 

대충 그런뜻인거 같은데, 저게 말이 되게 좋게 표현돼서 그렇지

실제로는 엘로퀀트 오알엠이랑 쿼리빌더만 사용할 수 있는거지.

그리고 중간에 

 

그래서 실제 코드에서는 get()을 한 후에 paginate()메소드를 사용하면 노랑불 나올거임.

 

요렇게!

엘로퀀트 빌더랑, 엘로퀀트 콜렉션이랑, 서포트 콜렉션에서 페이지네이트 메소드를 찾을 수 없다고......(털썩)

 

그리고 메뉴얼 페이지 중간에 보면 

Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually.

라고 한다. 그룹바이 안된데.

그룹바이 된거 쓰고 싶으면 만들어서 쓰래.

좡난하니 그룹바이 못잃어 나라 못잃어.

 

난 콜렉션을 뽑아서 데이터를 좀 더 정갈하게(?) 정리하고, 이것저것 한 담에 결과를 페이지네이트하고 싶어 하면

결국은 수동페이지네이션을 사용해야한다.

메뉴얼 페이지 보면, 기본 페이지네이터는 결과 총 갯수를 알 필요는 없지만

수동 페이지네이터는 결과 셋에 설정되어있는 전체 항목의 개수를 필요로 한데.

 

그래서 검색했더니, 

https://www.itsolutionstuff.com/post/laravel-6-paginate-with-collection-or-arrayexample.html

 

Laravel 7/6 Paginate with Collection or Array

laravel 7/6 paginate to array, laravel 7/6 paginate on array, laravel 7/6 pass array to pagination, laravel 7/6 pagination object array, laravel 7/6 pagination using array, laravel 7/6 create pagination from array

www.itsolutionstuff.com

이런게 있더라구 ㅎ

 

 

정확히

public function paginate($items, $perPage = 30, $page = null, $options = [])
{
	$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
    
	$items = $items instanceof Collection ? $items : Collection::make($items);
    
	return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), 
    					$perPage, $page, $options);
}

이 부분! 이부분을 사용해야함.

달리 손 볼곳 없이 그냥 가져다가 쓰면 된당.

parameter값을 보자.

 

이러고 쓰면 어떤 데이터들이 들어오더라도 콜렉션으로 만들어서 페이지네이션 해준당.

깔끔한 코드도 만조크!

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함