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

knubbe's avatar
Level 36

How to use model properties in nova action fields?

I have action button in laravel nova which I registry in nova resource. Is there a chance to pass model in action class to use his properties in action fields?

0 likes
18 replies
bugsysha's avatar

You get a collection of selected models in the handle method of the action. Is that what you are looking for? Your explanation is very hard to understand.

  public function handle(ActionFields $fields, Collection $models)
  {
    foreach ($models as $model) {
      (new AccountData($model))->send();
    }
  }
knubbe's avatar
Level 36

In nova resources, actions method, I define action class and use it onlyOnTableRow. In that action class in fields method I need to use model properties and somehow I need to pass model from resource class. For example:

// nova resouce
(new SomeAction($model))->onlyOnTableRow()

// some action
public function fields()
{
	...
	$this->model->property
	...
}
bugsysha's avatar

But when you call the action, you will get that model passed to it. Regarding fields, I have a feeling that you might be using them differently than what is expected. Action fields should be used by users to input some details that are not available in the system. So all data from the model is available later when action is running.

knubbe's avatar
Level 36

I understand but in this case in action fields I need to show default values for user in case he didn't provide anything

bugsysha's avatar

You do that later on when action is running, not before. Just use a note to say default values will be used.

knubbe's avatar
Level 36

I can't, that's required and default values depend on specific model/resource in table

bugsysha's avatar

So it is perfectly aligned with what I said.

knubbe's avatar
Level 36

I have some order signature action and order have some related departments and I want to inform user before execute signature action which managers he sending that order to sign

bugsysha's avatar

Just make the field that action is based on visible on index page and they can see it and put in the value based on it.

knubbe's avatar
Level 36

Sorry but I don't understand that approach

bugsysha's avatar

The index page is where all your resources are listed. There you can add any model property to be visible. Add the property so when they trigger the action they can see what the current value is and input what they want based on that.

knubbe's avatar
Level 36

I understand that part but that is not what I need and what I asked

bugsysha's avatar

But also there is nothing in the docs to support your use case so you are expecting something that is not supported.

1 like
DavidPetrov's avatar

You can achieve that, although in an a bit hacky way, by passing $this->resource to the action's constructor when registering the action on your resource. But be careful to check that the passed value is an actual model wherever you use it inside your action, because Nova will occasionally pass empty model/stdClass instances when handling the actions. But if your action is inline, you'll get the desired fields displayed.

2 likes
zgetro's avatar

@knubbe Thanks for hint.

In nova resources, action method, if you define a action class and use it onlyOnTableRow will only works on resource List view(aka Index). if you want same action on Detail view(aka read view) then you don't need to use onlyOnTableRow. but real problem is that in nova route nova-api/{resource}/actions?resourceId={number} it is not loading resource at all may be kind a bug in Nova!

As solution, in Resource file : app/Nova/{your-resource}.php

public function actions(Request $request)
    {
        return [
            new BookEventAction,
            (new AddAnonymousUserAction($this->model()))->showOnTableRow() // here $this->model() returns resource model.
        ];
    }

in Action file : app/Nova/Actions/{your-action}.php

class AddAnonymousUser extends Action
{
    ...

 public $model;

    function __construct($model = null)
    {
        
        $this->model = $model;

        if(!is_null($resourceId = request('resourceId'))){
            $this->model = Event::find($resourceId); //Event is my Resource class, you can make this one dynamic too.  nova/route/api.php get Actions route. ;)
        }
    }

   ...

  public function fields()
    {
       return [
            Select::make('Customer')
				->options(\App\Models\Customer::all()->pluck('company', 'id'))
                ->searchable(),
				->withMeta(['value' => optional($this->model)->customer_id], 			
       ];
     }
}

PS : snippit will give you idea about how nova route works. DON'T COPY+PASTE, my resource/action class is not same as yours.

7 likes

Please or to participate in this conversation.