Laravel Nova는 수많은 사용자 정의 옵션을 제공한다.

이러한 옵션 중 하나를 "조치"라고한다.

사용자 지정 작업을 수행하기 위해 리소스에 작업을 첨부하는 방법을 살펴 보겠다.


1. 조치 액션만들기(게시한 글)

1
php artisan nova:action PublishPost
cs



2. Nova/Actions/PublishPost.php

is_published 테이블 값을 0 (true)로 업데이트하는 소스이다.

1
2
3
4
5
6
7
8
    public function handle(ActionFields $fields, Collection $models)
    {
        foreach ($models as $model) {
            $model->update([
                'is_published' => true
            ]);
        }
    }
cs


Nova/Post.php 객체를 추가해준다.

1
2
3
4
5
6
    public function actions(Request $request)
    {
        return [
            new PublishPost       
        ];
    }
cs


3. 셀렉트 박스를 누르면 Select Action드롭박스가 활성화 된다.




되야하는데 변화가 없다 suss라고는 메시지가 나오는데 데이터베이스에 변화가 없음

https://nova.laravel.com/docs/2.0/actions/defining-actions.html#overview


안되는데 계속 진행하겠다 

2번항을 이렇게 다시 수정한다. Nova/Actions/PublishPost.php 정상적으로 DB에 입력이 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    public function handle(ActionFields $Fields, Collection $models)
    {
        // foreach ($models as $model) {
        //         $model->update(['is_published' => true]);
        // }
        foreach ($models as $model) {
            $model->forceFill(['is_published' => true])->save();
 
            $this->markAsFinished($model);
        }
 
        sleep(5);
 
        return Action::message('IS PUBLISHED가 변경 되었습니다.');
        //return Action::redirect('http://novapcb.test/nova/resources/posts');
    }
cs


4. 사용자 정의 메시지를 만들수 있다.

1
2
3
4
5
6
7
8
    public function handle(ActionFields $Fields, Collection $models)
    {
        foreach ($models as $model) {
                $model->update(['is_published' => true]);
        }
 
        return Action::message('The Post was published successfully');
    }
cs


메시지가 바뀐것을 확인 할 수 있다.

Action::danger 로 변경하면 메시지가 빨간색으로 변경된다.

Action::download(storage_path('your/file.csv'));

Action::redirect('http://novapcb.test/nova/resources/posts');


5. 업데이트 항목을 구현하는 것이다. 

Nova/Actions/PublishPost.php 

sleep(5) 목록수를 정해준다.

1
2
3
4
5
6
7
8
9
    public function handle(ActionFields $Fields, Collection $models)
    {
        foreach ($models as $model) {
                $model->update(['is_published' => true]);
        }
 
        sleep(5);
        //return Action::redirect('http://novapcb.test/nova/resources/posts');
    }
cs


Post.php 파일에서 아래와 같이 클래스안에서 임포트해준다.

1
2
3
4
class Post extends Model
{
 
    use Actionable;
cs


Posts 게시물의 디테일을 누르면 아래쪽에 변경 이력이 있을 것이다.


'라라벨 > NOVA' 카테고리의 다른 글

nova 맞춤도구 만들기  (0) 2020.01.04
nova 측정항목정의(value, trend, partition)  (0) 2020.01.04
nova 커스텀 테마  (0) 2020.01.02
nova main page 커스텀  (0) 2020.01.02
nova 유저 커스텀링크버튼 만들기  (0) 2020.01.02

+ Recent posts