HasanAswad's avatar

Laravel Nova Depending On ( Get Value From Feild to another field )

I have a model called OrderType where I save information of the order type. Here I am interested in a field called current_cost.

I have another model called Order where I add records of the orders. Here I am interested in a field called cost_per_order.

Order model linked via belongsTo with OrderType. OrderType model linked via hasMany with Order.

First I hide the field Order->cost_per_order as long as OrderType is null and not selected.

When I select an order type I want to:

  • Show Order->cost_per_order field
  • make its value with same value of OrderType->current_cost

Can I use dependsOn ? and how can I get the OrderType->current_cost to the field Order->cost_per_order ?

This is the fields inside Order Nova Model:

BelongsTo::make('Order Type', 'orderType', OrderType::class),

Number::make('Cost per Order', 'cost_per_order')
   ->dependsOn(
        ['orderType'],
                
        function (Number $field, NovaRequest $request, FormData $data) {

              if($data->orderType === null){
                    $field->hide();
              }

              $field
                    // How to make its value same as $data->orderType->current_cost ??
      }),
0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To achieve the desired functionality in Laravel Nova, you can use the dependsOn method to dynamically update the cost_per_order field based on the selected OrderType. Here's how you can implement this:

  1. Use the dependsOn method to listen for changes in the orderType field.
  2. Fetch the current_cost from the selected OrderType and set it as the value for cost_per_order.

Here's how you can modify your code:

use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Fields\FormData;

public function fields(Request $request)
{
    return [
        BelongsTo::make('Order Type', 'orderType', OrderType::class),

        Number::make('Cost per Order', 'cost_per_order')
            ->dependsOn(
                ['orderType'],
                function (Number $field, NovaRequest $request, FormData $data) {
                    if ($data->orderType === null) {
                        $field->hide();
                    } else {
                        // Fetch the OrderType model
                        $orderType = \App\Models\OrderType::find($data->orderType);

                        if ($orderType) {
                            // Set the value of cost_per_order to current_cost of the selected OrderType
                            $field->value = $orderType->current_cost;
                        }
                    }
                }
            ),
    ];
}

Explanation:

  • BelongsTo Field: This field is used to select the OrderType. It is linked to the OrderType model.
  • Number Field: The cost_per_order field is a number field that depends on the orderType field.
  • dependsOn Callback: Inside the callback function, we check if orderType is not null. If it's not null, we fetch the OrderType model using the ID from $data->orderType.
  • Set Value: If the OrderType is found, we set the value of the cost_per_order field to the current_cost of the selected OrderType.

This setup will ensure that the cost_per_order field is hidden until an OrderType is selected, and once selected, it will automatically populate with the current_cost from the OrderType.

1 like

Please or to participate in this conversation.