kirschbaum-development/nova-inline-relationship

belongsTo() inside hasMany()->inline() not showing up

Opened this issue · 13 comments

ratno commented

i have table like this:

  • users: laravel standard users
  • contact_category: home, work, other
  • user_contact: users_id, category_id, contact

user hasMany user_contact (belongsTo users, and belongsTo contact_category), when i use inline, the category not showing up.

this is contact_category form:
Screen Shot 2019-12-28 at 01 14 50

when using inline, the category select box is not showing up:
Screen Shot 2019-12-28 at 01 24 19

qXAdIlr

@ratno Thanks for submitting this issue. Would you please post the fields methods from each of the three resources? This would help us to recreate the issue as you have it. Thanks!

ratno commented

ContactCategory:
[ ID::make()->sortable(), Text::make('name'), ]

UserContact:
[ ID::make()->sortable(), BelongsTo::make("User","users",\App\Nova\User::class), BelongsTo::make("Category","category",\App\Nova\ContactCategory::class), Text::make("contact"), ]

Users, add inline hasmany:
HasMany::make("Contact","contact",\App\Nova\UserContact::class)->inline()

for more detail code it can be found here: ratno/nova-inline-sample@42a8d72

thanks very much,,

Same issue here

Jon78 commented

When I try to add the inline()-method to a HasMany-field we only get a few of the fields from the corresponding resource. For example only one Boolean when we have two. When I try to attach a new item I get more fields, but still not everything.

Same issue here

+1

The problem is here:

return collect($resource->availableFields(app(NovaRequest::class)))

getFieldsFromResource method extracts the fields that are showed up inside form and in detail page.
This reject rule:
collect(class_uses($field))->contains(ResolvesReverseRelation::class)
will exlude all BelongsTo field because they use ResolvesReverseRelation trait.

I think this rule is needed to exclude parent field on the child resource, but it's too aggressive and remove even all the ResolvesReverseRelation fields.

Same issue here.

Any potential workarounds for this?

This reject rule:
collect(class_uses($field))->contains(ResolvesReverseRelation::class)
will exlude all BelongsTo field because they use ResolvesReverseRelation trait.

I think this rule is needed to exclude parent field on the child resource, but it's too aggressive and remove even all the ResolvesReverseRelation fields.

Thank you @alberto-bottarini for putting me on track for deducing the problem. Unfortunately, the story thickens.

The ResolvesReverseRelation is too aggressive and I reduced its effectiveness by checking further for a comparison of the parent request resource through (app(NovaRequest::class))->resource() and comparing it with the $field->resourceClass. This works and effectively removes out the parent belongsTo relationship. In turn a recursive declaration of resources on creation is prevented.

The problem is that Nova is still rendering the field relations as the parent instead of as the child. This forces the method to check on the parent model rather than the child model. ie

class Parent extends Model
{
  public function children()
  {
    return $this->hasMany(Child::class);
  }
}

 class Child extends Model
{
  public function parent()
  {
    return $this->belongsTo(Parent::class);
  }
}

Load the create or edit page for the Parent Resource will render an error message of Call to undefined method App\Parent::parent()

I am working on a fork at https://github.com/frctnlss/nova-inline-relationship if anyone wants to help resolve this dependence issue.

I spent some time thinking I had this problem. But I specified the property foo_id instead of fooId and it worked.

Load the create or edit page for the Parent Resource will render an error message of Call to undefined method App\Parent::parent()

It looks like in the Nova code for \Laravel\Nova\Fields\Field under the function sortableUriKey it is getting the resource from the URL - which means the parent resource. I have no idea how this issue would go about being corrected.

Any Solution for this?