orlyapps / nova-belongsto-depend

Larave Nova BelongsTo Field with Dependcy
MIT License
182 stars 65 forks source link

Wrong resourceClass? #13

Closed noahlocke closed 5 years ago

noahlocke commented 5 years ago

I have a weird bug/issue. I have two nova resources, Notes and MyNotes. The only difference between the two is that MyNotes is filtered to only show the user "their" notes.

I'm using NovaBelongsToDepend on MyNotes and it works great.

When using it on Notes, it fails. The first field looks up just fine:

            NovaBelongsToDepend::make('User')
                ->options(\App\User::all())
                ->onlyOnForms(),

But the second, dependent field never gets any data:

            NovaBelongsToDepend::make('Client')
                ->optionsResolve(function ($user) {
                    return $user->clients()->get(['id','name']);
                })
                ->dependsOn('User')
                ->onlyOnForms(),

I checked and that's because there's nothing being sent back from the nova-belongsto-depend ajax request. I believe this is because it's using the wrong resourceClass:

{
   resourceClass: "App\Nova\MyNotes", 
   modelClass: "App\User", 
   attribute: "client", dependKey: 3
}

The resourceClass should be: App\Nova\Notes

I haven't ruled out some error on my end, but I've been at it for some time now and cannot for the life of me figure out why it's using that resourceClass in the ajax request???

noahlocke commented 5 years ago

The only thing I can think, is that since I'm using NovaBelongsToDepend on MyNotes as well, it's somehow being cached?

noahlocke commented 5 years ago

Further testing:

I placed the exact same code on MyNotes (client depends on user) and it works flawlessly. Before, I was only selecting client (since user is just $request->user()). But, I wanted to see if it was a problem with the code or with the resource, and it appears it's with the resource.

noahlocke commented 5 years ago

OK, sorry for all of these updates, but I think I've gotten closer to the bottom of this.

I commented out any reference to NovaBelongsToDepend on App\Nova\MyNotes. Next, I went back to test App\Nova\Notes. Sure enough, it errored-out stating that 'Client' didn't exist on App\Nova\MyNotes. So even though I'm on an entirely different Resource, it's looking at the other.

Somewhere the field, or Nova, is getting confused between these two resources.

noahlocke commented 5 years ago

After reviewing some other Nova issues, there seems to be other problems with the guessResource method... that would make a lot of sense. Sorry for the saga!