user1469734
user1469734

Reputation: 801

Nova Pivot BelongsToMany not working in table

I have the following Pivot table:

public function sheeps()
{
    return $this
       ->belongsToMany(Sheep::class, 'farm_sheep')
       ->withTimestamps()
       ->withPivot(['weight', 'foobar']);
}

Now I want a BelongsToMany field that shows the values that are in the pivot Table, so: weight and foobar. But when I do that, it just shows the table with the data from the Sheep Nova resource. So not that pivot tables values. What am I doing wrong

BelongsToMany::make('Sheeps')
    ->fields(function () {
        return [
            Number::make('Weight'),
            Textarea::make('Foobar'),
        ];
    }),

Upvotes: 0

Views: 1979

Answers (2)

Ben
Ben

Reputation: 62384

Even having defined both sides of the relationship, I was not able to get this to function as intended. What worked for me was adding a pivot field to the original resource's fields() output:

    public function fields(NovaRequest $request)
    {
        return [
            Text::make('Type'),
            Boolean::make('Premium', 'pivot.premium'),
        ];
    }

Upvotes: 0

Kenny Horna
Kenny Horna

Reputation: 14241

You need to define those fields in both ends of the relationship. From the Nova docs:

Once these fields are attached to the relationship field, and the relationship has been defined on both sides, they will be displayed on the related resource index.

Upvotes: 2

Related Questions