필터 및 렌즈 (nova Filters and Lenses)

필터 및 렌즈 (nova Filters and Lenses)

Laravel Nova의 사용자 정의 필터 및 "렌즈"를 사용하여 리소스 목록을 사용자 정의하는 방법을 배운다.

1. 출판되었는지 안되었는지 구분하는 필터를 생성한다.

1 php artisan nova:filter PostPublished cs

아래와 같이 Nova/Filters/PostPublished.php 파일이 생성되었다.

2. Nova/Filters/PostPublished.php 파일을 아래와 같이 수정한다.

1 2 3 4 5 6 7 public function options(Request $request) { return [ 'Is Published' => '1', 'Not Published' => '0' ]; } Colored by Color Scripter cs

1 2 3 4 public function apply(Request $request, $query, $value) { return $query->where('is_published', $value); } Colored by Color Scripter cs

3. Nova/Post.php 파일에서 filters메서드에서 소스를 작성한다. 객체를 생성해준다.

1 2 3 4 5 6 7 8 public function filters(Request $request) { return [ new PostPublished ]; } Colored by Color Scripter cs

IS PUBLISHED(출판)이 찍히면 1 아니면 0

4. 이번에는 카테고리 필터를 생성해준다.

1 php artisan nova:filter PostCategories cs

Nova/Filters/PostCategories.php

1 2 3 4 5 6 7 public function options(Request $request) { return [ 'Tutorials' => 'tutorials', 'News' => 'news' ]; } Colored by Color Scripter cs

1 2 3 4 public function apply(Request $request, $query, $value) { return $query->where('category', $value); } Colored by Color Scripter cs

Nova/Post.php 파일에서 filters메서드에서 소스를 작성한다. new PostCategories 객체를 생성해준다.

1 2 3 4 5 6 7 8 9 10 public function filters(Request $request) { return [ new PostPublished, new PostCategories ]; } Colored by Color Scripter cs

POST CATEGORIES 필터가 추가된것을 볼 수 있다.

와우~ 두가지이상 항목을 필터를 할 수 있다니~~

5. 렌즈파일 생성

1 php artisan nova:lens MostTags cs

Lenses/MostTags.php 파일이 생성되었다.

MostTags.php 파일에서 다음과 같이 소스를 수정한다.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public static function query(LensRequest $request, $query) { return $request->withOrdering($request->withFilters( $query ->withCount('tags') ->orderBy('tags_count', 'desc') )); } /** * Get the fields available to the lens. * * @param \Illuminate\Http\Request $request * @return array */ public function fields(Request $request) { return [ ID::make('ID', 'id')->sortable(), Text::make('Title'), Number::make('# Tags', 'tags_count') ]; } Colored by Color Scripter cs

Nova/post.php 파일에 객체를 넣어준다.

1 2 3 4 5 6 public function lenses(Request $request) { return [ new MostTags ]; } Colored by Color Scripter cs

Lens 라는 드롭다운 버튼이 생성되었다.

해당 타이틀의 태그와 수를 확인 할 수있다.

from http://anko3899.tistory.com/290 by ccl(A) rewrite - 2021-10-28 02:01:09