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

alexwenzel's avatar

wrong number of models for an action

Hi, this drives me crazy, but when I use this specific action, it always gives me ALL models from the table, not only the selected row.

resource:

public function actions(NovaRequest $request): array
    {
        return [
            (new AssignTowingService)
                ->canSee(function ($request) {
                    return true;
                })
                ->canRun(function ($request, $user) {
                    return true;
                })
                ->onlyOnTableRow(),
        ];
    }

action:

public function handle(ActionFields $fields, Collection $models)
    {
        /** @var Process $model */
        foreach ($models as $model) {
            $model->state = ProcessStates::IN_PROGRESS;
            $model->save();
        }

        return Action::message('...');
    }

$models is never only the clicked current row. Any ideas?

0 likes
2 replies
emiel84's avatar

I had the same problem. In my case the cause was a wrong query in the indexQuery method of the Nova Resource.

1 like
alexwenzel's avatar

I ended up doing someting like this:

    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields  $fields
     * @param  \Illuminate\Support\Collection  $models
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {
        if ($models->count() > 1) {
            return Action::danger('error');
        }
        $models->first()->setInProgress();
        return Action::message('success');
    }

Please or to participate in this conversation.