Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

laravel nova - Trouble understanding where to register fields

I am installing this Nova package: https://novapackages.com/packages/classic-o/nova-media-library and am having trouble figuring out where to put this block of code:

Add field to the resource:

use ClassicONovaMediaLibraryMediaLibrary;

class Post extends Resource
{
    ...
     public function fields(Request $request)
        {
            return [
                ...
                MediaLibrary::make('Image'),
                ...
            ];
        }
    ...
}

I've looked around, but am very new to Laravel, so I'm just a little confused. I understand that it is a field, so I've looked here: https://nova.laravel.com/docs/1.0/resources/fields.html#place-field, but don't really understand the location where these can be registered.

I appreciate any insight or context here. Thank you!

question from:https://stackoverflow.com/questions/65837056/trouble-understanding-where-to-register-fields

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With Laravel Nova, they tried to make everything as easy as possible. So, a Resource is what adds all the functionality to your Model, and a Model represents (in some way) a table in your database. A resource is just a class that extends from Resource, and has some functions to be completely functional.

So, in your example, you should have a Model called Post, the code showing is an incomplete Resource called Post (that's why you se those three points before and after the function). As you read the Resource documentation, you will see that the function fields() always returns an array of Fields. The code

MediaLibrary::make('Image')

is, indeed, a field, that's why it's inside de array. The code that you have is just an example, and it's purpose is to illustrate that the MediaLibrary field can be used as any other field. I strongly recommend that you take enough time to read about Models, Eloquent, Resources and Fields.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...