nova 측정항목정의(value, trend, partition)

nova 측정항목정의(value, trend, partition)

관리 패널을 생각할 때 염두에 둬야 할 것은 응용프로그램의 주요 통계를 신속하게 계산하고 검토 할 수 있다는 것입니다. Laravel Nova는 메트릭을 추가하고 구성하는 프로세스를 가능한 한 쉽게 만든다. 작동 방식을 알아보자

1. Posts의 카운터를 알아보기 위해 명령어를 입력한다.

1 php artisan nova:value PostCount cs

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

2. Post모델을 작성해주고 use App\Post 를 임포트해준다.(모델을 임포트)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 count($request, Post ::class); } Colored by Color Scripter cs

average : id(테이블명)

1 return $this->average($request, Post::class, 'id'); cs

sum

max

min

참고

https://nova.laravel.com/docs/2.0/metrics/defining-metrics.html#value-metrics

3. app/Providers/NovaServiceProvider.php 파일의 cards()메서드로 가자

new Help, 카드는 사진과 같은 부분을 말한다. 주석처리하면 보이지 않게 된다.

Metrics/PostCount.php 인스턴스를 넣어준다.

1 2 3 4 5 6 7 protected function cards() { return [ //new Help, new PostCount ]; } cs

아래처럼 통계가 나온다.

4. trend 파일을 생성해준다.

1 php artisan nova:trend PostsPerDay cs

Nova/Metrics/PostsPerDay.php 파일이 생성되었다.

모델명으로 변경해준다.

countByDays

1 2 3 4 public function calculate(NovaRequest $request) { return $this->countByDays($request, Post::class); } Colored by Color Scripter cs

아래는 메소드 종류이다 골라서 사용하자

https://nova.laravel.com/docs/2.0/metrics/defining-metrics.html#trend-query-types

1 2 3 4 5 return $this->countByMonths($request, User::class); return $this->countByWeeks($request, User::class); return $this->countByDays($request, User::class); return $this->countByHours($request, User::class); return $this->countByMinutes($request, User::class); cs

5. app/providers/NovaServiceProvider.php 에서 인스턴스를 추가해준다.

1 2 3 4 5 6 7 protected function cards() { return [ new PostCount, new PostsPerDay ]; } cs

Trend가 추가되었다.

뒤에 showLatestValue()를 추가하면 매월 마지막날의 수량을 보여줄 수 있음

1 2 3 4 5 public function calculate(NovaRequest $request) { return $this->countByMonths($request, Post::class) ->showLatestValue(); } Colored by Color Scripter cs

포멧형식을 정의 할수도 있음

1 ->format(0,0) cs

접두사를 설정할 수 있음

접두사 prefix

접미사 suffix

1 2 3 4 5 public function calculate(NovaRequest $request) { return $this->countByMonths($request, Post::class) ->showLatestValue() ->prefix('ppm') ; } Colored by Color Scripter cs

string도 정의 할 수 있다.

1 public $name = "게시판 게시글 수"; cs

이렇게도 가능

1 2 3 4 public function name() { return '게시판 게시글 수'; } cs

직접 값을 넣을 수 도 있음

1 2 3 4 5 6 7 8 9 10 public function calculate(NovaRequest $request) { // return $this->countByMonths($request, Post::class) // ->showLatestValue()->suffix('ppm'); return (new TrendResult())->trend([ 'Day 1' => 1, 'Day 2' => 200, 'Day 3' => 50 ]); } Colored by Color Scripter cs

7. partition 파일을 생성한다.

1 php artisan nova:partition PostsPerCategory cs

아래처럼 파일이 생성되었다.

Nova/Metrics/PostsPerCategory.php 소스변경 여기서는 카테고리 테이블을 넣어주었다.

1 2 3 4 public function calculate(NovaRequest $request) { return $this->count($request, Post ::class, ' category '); } Colored by Color Scripter cs

8. app/providers/NovaServiceProvider.php 에서 인스턴스를 추가해준다.

1 2 3 4 5 6 7 8 protected function cards() { return [ new PostCount, new PostsPerDay, new PostsPerCategory ]; } cs

파티션이 추가되었다.

posts 리소스에 넣을 수 도 있다.

Nova/Post.php 파일을 열어보면 cards메서드가 있는데

그곳에 인스턴스를 넣기만 하면 된다.

화면 그리드를 변경할 경우

1 2 3 4 5 6 7 8 9 public function cards(Request $request) { return [ (new PostsPerDay())->width('full'), (new PostCount())->width('1/2'), (new PostsPerCategory())->width('1/2') , ]; } Colored by Color Scripter cs

아래와 같이 그리드가 적용된 것을 볼 수 있다.

from http://anko3899.tistory.com/296 by ccl(A) rewrite - 2021-10-27 20:00:33