Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JellowGerard's avatar

Default value for Nova's Benjacho\BelongsToManyField

Hi, For my Laravel Nova resource, I'm using Benjacho\BelongsToManyField to allow for quickly filling a pivot table, which work great, except that it seems that I cannot set a default for new parent records.

It does not throw an error when I'm adding ->default(something) to the field call though, it simply is ignored. Am I missing something? I tried with the relation record ID, as well as the whole row (see below), but both have no result it seems.

BelongsToManyField::make('my relation name', 'my_relation_reference', 'App\Nova\Relation')
                        ->canSelectAll()
                        ->default(\App\Models\Relation::where('default', true)->get())
                        ->required()
                        ->rules('required', 'min:1'),

Hopefully someone can help me out with this. Many thanks in advance :-)

0 likes
1 reply
JellowGerard's avatar

For those of you hitting the same issue: I found the following work-around:

BelongsToManyField::make('my relation name', 'my_relation_reference', 'App\Nova\Relation')
                        ->canSelectAll()
                        ->resolveUsing(function() {
                            return $this->relation()->exists() ? $this->relation : Relation::where('default', true)->get();
                        })
                        ->required()
                        ->rules('required', 'min:1'),

This sets it correctly on creations, while not overwriting the stored values on editing on displaying.

Please or to participate in this conversation.