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

Synchro's avatar

Nova pivot relations

I asked about this a year ago. Things have changed a little since (it's no longer hanging, and I'm on latest Nova), but there is still an issue. When the pivot relation is displayed in a list view, it renders correctly. When it's shown on a form, it is empty and has no options to select from. The main Scan resource contains this definition:

            BelongsToMany::make('Products')
                ->searchable()
                ->readonly()
                ->fields(
                    function ($request, $relatedModel) {
                        return [
                            BelongsTo::make('Orderable')
                                ->nullable(),
                        ];
                    }
                ),

and the ProductScan pivot model contains this relation:

public function orderable(): BelongsTo
    {
        return $this->belongsTo(Orderable::class);
    }

The BelongsTo field type doesn't let me specify options() to build an options list manually, so that's no use as a workaround.

Is it the case that Nova doesn't support relations on pivots?

0 likes
2 replies
LaryAI's avatar
Level 58

Nova does support relationships on pivot models, but there are some nuances to how you can work with them, especially when it comes to displaying and editing them in Nova's UI.

From your description, it seems like you are trying to define a relationship within the pivot fields of a BelongsToMany relationship. The BelongsTo field inside the pivot fields should automatically pick up the related models if the relationship is defined correctly in your Eloquent models.

Here's a step-by-step guide to ensure you have everything set up correctly:

  1. Make sure your Orderable model is defined correctly and has the inverse relationship to ProductScan.
// Orderable.php
class Orderable extends Model
{
    public function productScans()
    {
        return $this->hasMany(ProductScan::class);
    }
}
  1. Ensure that your ProductScan pivot model is set up correctly with the orderable relationship.
// ProductScan.php
use Illuminate\Database\Eloquent\Relations\Pivot;

class ProductScan extends Pivot
{
    public function orderable(): BelongsTo
    {
        return $this->belongsTo(Orderable::class);
    }
}
  1. In your Scan model, make sure the products relationship is defined correctly and that it uses the ProductScan pivot model.
// Scan.php
class Scan extends Model
{
    public function products()
    {
        return $this->belongsToMany(Product::class)
                    ->using(ProductScan::class)
                    ->withPivot('orderable_id');
    }
}
  1. In your Nova Scan resource, you should define the BelongsToMany field like this:
// Scan.php (Nova Resource)
use Laravel\Nova\Fields\BelongsToMany;

BelongsToMany::make('Products')
    ->searchable()
    ->fields(function ($request) {
        return [
            BelongsTo::make('Orderable')
                ->nullable()
                ->searchable(),
        ];
    }),
  1. Make sure that you have a corresponding Nova resource for Orderable and that it is registered in your NovaServiceProvider.
// Orderable.php (Nova Resource)
use Laravel\Nova\Resource;

class Orderable extends Resource
{
    // ...
}
  1. If you want to manually specify options for the BelongsTo field, you can use the options method, but this is not typically necessary if your Eloquent relationships are set up correctly.
BelongsTo::make('Orderable')
    ->nullable()
    ->searchable()
    ->options(Orderable::all()->pluck('name', 'id')),

If after following these steps, you still encounter issues with the BelongsTo field in the pivot not displaying options, you may need to debug further by checking the following:

  • Ensure that there are Orderable records in the database to be displayed.
  • Check for any JavaScript errors in the browser console when loading the Nova form.
  • Make sure you are not encountering any policy or authorization issues that might prevent the records from being displayed.

If everything is set up correctly, Nova should be able to handle the pivot relationship without any additional workarounds. If you continue to have issues, it might be worth checking the Nova documentation or reaching out to the Nova community for further assistance.

Synchro's avatar

@LaryAI This sounded good. I was missing the first of those relations (it's not normally needed, but it is harmless to add it), but it didn't change anything. I tried adding options() to the BelongsTo, but there doesn't seem to be any such method.

Please or to participate in this conversation.