I figured it out. For some reason, when using the fieldsForDetail() method, you have to also include the BelongsToMany field in the fields() method of the resource class, otherwise it throws this error.
BelongsToMany field "Attempt to read property "resourceClass" on null"
I know this question has been discussed both here and on the Nova Issues github before, but the proposed solutions aren't working for me, and I can't figure out what I'm doing wrong.
Two models have a belongs-to-many polymorphic relationship. I have "Providers" (like a business) that belongs to many counties, and a county can belong to many Providers.
Provider relationship:
public function counties(): BelongsToMany
{
return $this->belongsToMany(County::class, 'counties_served')->withTimestamps();
}
County relationship:
public function providers(): BelongsToMany
{
return $this->belongsToMany(Provider::class, 'counties_served')->withTimestamps();
}
Provider Nova Resource:
public function fieldsForDetail(NovaRequest $request): array
{
return [
/* ... */
BelongsToMany::make('Counties Served', 'counties', County::class),
];
}
County Nova Resource:
public function fields(NovaRequest $request): array
{
return [
/* ... */
BelongsToMany::make('Providers', 'providers', Provider::class),
];
}
When I try to attach a county to a provider, the page loads but the counties do not load, and I get a 500 error for the 'attachable' route:
https://nova.test/nova-api/providers/10/attachable/counties?search=&first=false&withTrashed=false&viaRelationship=counties
I've tried changing the uriKey on the County resource to be county and counties and County and Counties to no luck, and i'm not sure what's going wrong with this relationship that Nova can't find the resource data.
The error Nova gives is Attempt to read property "resourceClass" on null, and I can't find anything more detailed than that in the stack trace.
Note: I neglected to mention that the error only appears when attaching a County to a Provider, but when attaching a Provider to a County, the relationship and Nova work exactly as they should, without issue.
Please or to participate in this conversation.