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

garrettmassey's avatar

Nova + MorphMany + MorphOne required relationship on resource creation

I'm not sure if I'm setting up the Nova fields incorrectly or if I'm setting up the eloquent relationships incorrectly for this project, but what I'm trying to do is this:

I have three models I'm trying to work with: Customer, Business, and Address.

The Customer has many addresses, and has one Primary address. A Business has many addresses and has one primary address.

public function addresses(): MorphMany
{
    return $this->morphMany(Address::class, 'addressable');
}
public function primaryAddress(): MorphOne
{
    return $this->morphOne(Address::class, 'addressable')
        ->where('type', AddressType::PRIMARY_ADDRESS->value);
}

And what I want to do in Nova is that when a new customer is being created, a primary address is required to be created along with the customer. Same applies to the business.

Customer Nova Create Code

 public function fieldsForCreate(NovaRequest $request): array
{
    return [
        Select::make('Status', 'status')
            ->options(ClientStatus::getOptions())
            ->default(ClientStatus::ACTIVE->value)
        Text::make('Name')
            ->rules('required', 'max:255'),
        MorphOne::make('Primary Address', 'primaryAddress', Address::class)
            ->required(),
    ];
}

Address Nova Code:

public function fields(NovaRequest $request): array
{
    return [
        MorphTo::make('Belongs To', 'addressable')->types([
             Business::class,
             Customer::class,
        ])->sortable(),
        Text::make('Cross Streets')->sortable(),
		/* ... */
    ];
}

And when I go to create a new customer, the required address field displays the addressable_type and addressable_id dropdowns, which I don't want. I want those parts of the relationship to populate automatically after the new customer is created.

So, how can I get Nova to not require the addressable_type and addressable_id fields when creating a new customer + address?

0 likes
0 replies

Please or to participate in this conversation.