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

danikldd's avatar

BelongsTOManyField nova 4

Hello,

I have a relationship made between 2 tables products and orders and a pivot table.


    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function products(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
    {
        return $this->belongsToMany(Product::class, 'orders_products', 'order_id', 'product_id');
    }

 Schema::create('orders_products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('order_id')->unsigned();

            $table->bigInteger('product_id')->unsigned();

            $table->foreign('order_id')->references('id')->on('orders')

                ->onDelete('cascade');

            $table->foreign('product_id')->references('id')->on('products')

                ->onDelete('cascade');
        });

Now my question is: How can I add a select in nova 4 for many to many? I tried this field

return [
            ID::make()->sortable(),
            Text::make('Total comanda', 'total'),
            Text::make('Locatie', 'location'),
            Text::make('Mesaj de la client', 'message_from_client'),
            HasOne::make('Utilizator', 'user_id', User::class),
            BelongsToManyField::make('Produse', 'products', Product::class)
                ->optionsLabel('name')
                ->options(Product::all())
                //->rules('required', 'min:1'),

        ];

The problem is that it no longer works in nova 4 and I get an error. Any other ideas?

0 likes
4 replies
danikldd's avatar

@MohamedTammam message : "Call to undefined method App\Models\Product::uriKey()" trace : [{file: "/var/www/html/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php",…},…]

Please or to participate in this conversation.