michielkempen / nova-polymorphic-field

A Laravel Nova field that allows you to create a collection of polymorphic resources.
MIT License
68 stars 14 forks source link

Use nova resource for fields #2

Closed dillingham closed 2 years ago

dillingham commented 6 years ago

Any chance the fields could be automatically detected from the resource? like morphTo

MorphTo::make('Commentable')->types([
    \App\Nova\Post::class,
    \App\Nova\Video::class,
])
michielkempen commented 6 years ago

Hi @dillingham, there is a fundamental difference between the MorphTo field and the PolymorphicField.

The MorphTo field basically creates a link between two models, which each have their fields defined in their own Resource classes.

The PolymorphicField also creates a link between two models, but uses only one Resource class in which all the fields are defined.

dillingham commented 6 years ago

I understand. Im saying that if you were to have resources for Article and Video that it would be much cleaner to define them once and not in their resource and the post resource.

Wanting to see just articles or just videos will happen in some apps Wanting to see all comments but also see articles and videos separately

It is a great package for quickly creating the relationship But I really think it needs to use their fields like the MorphTo approach

dillingham commented 6 years ago
PolyField::make('Type')->types([
    \App\Nova\Video::class,
    \App\Nova\Article::class
])
PolyField::make('Commentable')->types([
    \App\Nova\Video::class,
    \App\Nova\Article::class,
    \App\Nova\Event::class,
    \App\Nova\Listing::class,
    \App\Nova\Item::class,
])

I may not fully understand this package honestly lol 😅

m2de commented 5 years ago

Hi @michielkempen . Have you got any more thoughts on this?

My first thought was also that it would be nicer to pass Nova resources into the types instead of re-defining the fields, like @dillingham has described.

nenads commented 5 years ago

Hi,

There is small workoround for it.

Start using https://nova.laravel.com/docs/1.0/resources/relationships.html#morphtomany part of

MorphToMany::make('Users')->fields(new TaggableFields) where TaggableFields is class that you can reuse

image

Then call it like this, yes it is dirty as hell :)

image

milewski commented 4 years ago

@dillingham I think this is just what you are (were) looking for: https://github.com/dcasia/nova-inline-morph-to