Skip to content

Nova Api not defined when belongToMany is not defined in fields #6887

@GoatFreezy

Description

@GoatFreezy
  • Laravel Version: 12.19.3
  • Nova Version: 5.7.3
  • PHP Version: 8.4.7
  • Database Driver & Version: PgSql 17
  • Operating System and Version: Win 11 running app with sail
  • Browser type and version: Firefox 139.0.4
  • Reproduction Repository: https://github.com/###/###

Description:

Hello,

I've multiple model that have a many to many relationship and wanted to be able to associate models in the detail section of a given model.
Thus i've defined only in the fieldForDetail function my belongToMany relationship and it generated correctly the list in my detail page.
But there is only the delete functionnality that is working.
When trying to create or update a relation the select field if empty
Cf a create example:

Image

But if i define it also in the field function the API is now responding.
It took my several days to understand this because i didnt wanted to duplicate code and when trying nova for the first time i instantly used the dedicated function for everypage (detail, index) and only used field for my forms.

Detailed steps to reproduce the issue on a fresh Nova installation:

Create a a model with a ManytoMany relation to User.php

    /**
     * Achievements relation (many-to-many with pivot data)
     */
    public function achievements(): BelongsToMany
    {
        return $this->belongsToMany(Achievement::class);
    }

In Achivement.php

    public function users(): BelongsToMany
    {
        return $this->belongsToMany(User::class);
    }

Nova\User.php

    public function fieldsForDetail(NovaRequest $request): array
    {
        return [
            BelongsToMany::make('Achievements', 'achievements', Achievement::class),
        ];
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions