1. 마이그레이션에서 posts 의 컬럼을 추가하는 명령어를 입력하자

1
php artisan make:migration --table=posts add_more_post_columns
cs



2. 마이그레이션을 작성한다.

1
2
3
4
5
6
7
8
9
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->datetime('publish_at')->nullable();
            $table->datetime('publish_until')->nullable();
            $table->boolean('is_published')->default(false);
            $table->string('category')->nullable();
        });
    }
cs


마이그레이션을 실행해준다.


3. Nova/Post.php 파일을 열어 소스를 수정해준다.


필드타입보기> https://nova.laravel.com/docs/2.0/resources/fields.html#field-types


아래와 같이 필드를 추가해보자

sortable() = 목록에서 정렬을 할 수 있게 해준다.




대시보드로 가서 새로고침하면 아래와 같이 변경된 것을 확인할 수 있다.




Post.php 파일에서 정의 해주자

1
2
3
4
5
6
    protected $casts = [
 
        'publish_at' => 'datetime',
        'publish_until' => 'datetime'
 
    ];
cs



4. Post.php 파일에 다시 필드를 추가 해준다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
 
            Text::make('Title')->sortable(),
 
            Trix::make('Body')->sortable(),
 
            DateTime::make('Publish At')->sortable(), //public_at
 
            DateTime::make('Publish Until')->sortable(), //public_at
 
            Boolean::make('Is Published'),
 
 
        ];
    }
cs


필드가 추가 되었다.


Boolean 체크박스가 적용되었다.


5. 이제 카테고리필드를 만들어보자

1
2
3
4
5
6
            Select::make('Category')->options([
 
                'tutorials' => 'Tutorials',
                'news' => 'News'
 
            ])
cs



5.1 카테고리 업데이트를 못하게 막으려면 hideWhenUpdating() 메서드를 넣어주자

1
2
3
4
5
6
            Select::make('Category')->options([
 
                'tutorials' => 'Tutorials',
                'news' => 'News'
 
            ])->hideWhenUpdating()
cs


업데이트시 카테고리가 보이지 않게 되었다. 하지만 글쓰기를 하면 정상적으로 보여진다.


필드숨기는 기능 메뉴얼:

https://nova.laravel.com/docs/2.0/resources/fields.html#showing-hiding-fields



6. 필드가 너무 많아서 목록에서 숨기고 싶은 경우


필드 뒤에 hideFromIndex() 메서드를 추가한다.

1
2
3
            DateTime::make('Publish At')->hideFromIndex(), 
 
            DateTime::make('Publish Until')->hideFromIndex(), 
cs


이제 필드에서 보이지 않게 되었다.


7. 6번의 반대로 수정에서 보이지 않게 하려면 onlyOnIndex() 메서드를 넣어주면 됨

1
Boolean::make('Is Published')->onlyOnIndex(),
cs


블리언 체크하는 컬럼이 사라졌다.


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

자원(resource)의 권한(Authorization) policy(정책)  (0) 2020.01.01
자원(resource)의 검증(vaildation)  (0) 2020.01.01
자원의(resource) 관계(Relationships)  (0) 2020.01.01
자원(resource)의 정의  (0) 2019.12.31
nova설치하기  (0) 2019.12.31

+ Recent posts