plugin
plugin copied to clipboard
[Bug]: Incorrect path to resource in relationship fields in resource
Bug description
When generating a nova resource, on all related fields, the class referred to is to the model, instead of the resource.
Code currently generated for a has many field
HasMany::make('Shows', 'shows', Show::class),
also a use statement is added
use App\Models\Show;
The code generated should be
HasMany::make('Shows', 'shows', ShowResource::class),
Plugin version
No response
Operating system
No response
Steps to reproduce
No response
Relevant log output
No response
Thanks for posting, Tom. What if there is no resource for relation yet? Better to miss it?
@adelf Yes, I did think about that problem, and I agree that I would rather have the code generated with potentially incorrect code than not generated at all.
I was wondering if maybe you could rely on convention instead? Even if a resource for the related object might not be created yet (although I do try to create all mine starting with the related resources first), the plugin does know the convention I am using by what info I put in the fields in the ui box.
ie, for posts with categories, I fill in the resource name (PostResource
) and the class name (App/Models/Post
).
It can deduce that I am naming my resources based on the model name, appended with the word Resource.
That seems to be a safer guess that using the model as the third parameter in the HasMany::make(label, relationship, resource)
field.
If Laravel Idea guesses wrong in the Nova Resource name, it will throw a more obvious error (class not found), vs an error that tripped me up for a while (method not found uriKey() on model)
Anyway, its not a deal breaker either way. Its easy enough to change the code after generation :-) I just wanted to point it out, as it is one of the few things that actually throws an error after Laravel Idea generates my Nova resource code.
I installed the plugin yesterday and i also have this problem.
My suggestion is: Make all the same names. Better than using the model name in the relations.
public static $model = \App\Models\App::class; HasMany::make('Locations', 'locations', \App\Nova\Location::class),
Actually i have to rewrite every nova resource.
Fixed in 8.0.