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

fernandoff's avatar

Laravel Nova 4 belongsToMany bug with "depends on"

I have an Order wich has and belongs to many products.

When i change the product (during an attach model operation) i want to change the product price to save on the order_product pivot table.

Tried many diferent ways but just wont work.

Only works when the page first loads and after i saved the 'order_product' register. At this point you cant change the product anymore, so dont make sense anyway.

Also would be nice to be able to change the product after saved the pivot/attached.

Product(id, description, price) Order(id, user_id, estimated_delivery_date) Order_Product(id, order_id, product_id, quantity, price) Want the price here to keep the historic price payed for a given order

class Order extends Resource{
   public function fields(NovaRequest $request)
    {
        return [

ID::make()->sortable(),
BelongsTo::make('User')->searchable(),
Date::make('Estimated Delivery', 'estimated_delivery_date'),

 BelongsToMany::make('Products')
	->fields(function ($request, $relatedModel) {
		return [
			//ID::make('product_id', 'product_id'),
			Number::make('Quantity')->rules('required', 'numeric'),
			Text::make('Price')
				->dependsOn(
					['Products'], // Products, product_id, resource:products
					function (Text $field, NovaRequest $request, FormData $formData) {
						if(!empty($field->price)){
							ds('ja tem valor ' . $field->price);
						} else {
							if(!empty($formData['resource:products'])){
								$produto = \App\Models\Product::where('id', $formData['resource:products'])->first();
								$field->value = $produto->price ?? 0;
							}
						}
					}
				),
		];
	})
	->searchable()
	->showOnCreating()
];

];
}
0 likes
0 replies

Please or to participate in this conversation.