Search code examples
javascriptarrayslaravellaravel-nova

Laravel (7.x) nova (3.10) action - get current model instance on fields()


i have simmilar question to this Laravel nova action - get current model instance on fields()

but with info from https://github.com/laravel/nova-issues/issues/1813

It is possible.

I use Laravel 7.x and nova 3.10

I have a resource with field "size" for example 3KG, 4KG

I have an action with (only on index rows) field "amount" (number field)

I want to user to be able only to put multiplication of "size" in "amount" and a field that display on action value of "size"

I tried in action fields:

    Text::make(__('Package'), 'size')
->default(function(){

//need to load value of current model resource "size" to inform user

        })
        ->readonly(),

    Number::make(__('Amount'), 'amount')
        ->sortable()
        ->min(1) // -> i need to change this based on "size" form model resource
        ->max(9999)
        ->step(1) // -> i need to change this based on "size" form model resource
        ->updateRules('nullable')
        ->rules('required', 'number')
        ->help(__('in kilograms')),

How to load this JS array "ConfirmActionModal['selectedResources'][0]" in laravel fields?

I'm not well in JS, how to load values into php ?


Solution

  • get model name:

    $model = $request->viaResource;
    

    get model ID:

    $id = $request->viaResourceId;
    

    example:

    $post = $parent = \App\Models\Post::find($request->viaResourceId);