6

I have a Nova Resource named "Partner" with fields menthod:

public function fields(Request $request)
{
    return [
        Text::make('Name*', 'name')->rules('required')->sortable()->onlyOnForms(),
        Text::make('Name', 'name')->sortable()->exceptOnForms(),

        BelongsTo::make('Rate*', 'customrate', 'App\Nova\Rate')
            ->onlyOnForms()->nullable(),
        BelongsTo::make('Rate', 'customrate', 'App\Nova\Rate')
            ->searchable()->exceptOnForms(),
    ];
}

Also, I have a related Nova resource named "Rate" with its respective fields method:

public function fields(Request $request)
{
    return [
        ID::make()->sortable(),

        Text::make('Name*', 'name')->rules('required')->onlyOnForms(),
        Text::make('Name', 'name')->exceptOnForms(),

        Text::make('Flat Fee*', 'flat_fee')->rules('required')->onlyOnForms(),
        Text::make('Flat Fee', 'flat_fee')->exceptOnForms(),

        HasMany::make('Partner*', 'partner', 'App\Nova\Partner')->onlyOnForms(),
        HasMany::make('Partner', 'partner', 'App\Nova\Partner')->exceptOnForms(),
    ];
}

Now, I can add a "Rate" to a "Partner" from Partner`s form.

enter image description here

But now, client wants to be able to add multiple "partners" from "Rate"`s edit form. How to make this possible? Any ideas?

Now Nova gives me option to create new multiple Partners under Rate, but not to attach already existing Partners to Rate.

Vineeth Vijayan
  • 1,215
  • 1
  • 21
  • 33
  • To make it possible, internally, there should be a many to many pivot table like `partner_rate`. For transitioning, you will have to insert existing entries via a SQL query or laravel migration. – Bharat Geleda Feb 26 '19 at 20:47

1 Answers1

2

You can't attach anything to HasMany. There is issue/request for it https://github.com/laravel/nova-issues/issues/520

Current possible solution it is use BelongsToMany with pivot table.

Sanasol
  • 892
  • 8
  • 24
  • But that still doesn't add the field to the edit/create screens?! – Marcus Christiansen Dec 07 '19 at 05:28
  • @MarcusChristiansen ofc not since HasMany by design not simple single field :) You can use this to add nested relations form to parent model form https://github.com/yassipad/laravel-nova-nested-form Works like a charm for me. – Sanasol Dec 09 '19 at 11:53