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

lara128198's avatar

How to access all of chunked model collections within my action

I have a 'print badges' action, which I am using to acquire all of the selected user resource ids which I then attach to the query string of a redirect to a domPDF script - everything is working great, except I have just noticed that only the last portion of user models are visible within my action - that lead me to https://github.com/laravel/nova-issues/issues/1112, which seems to suggest that instead of using handle() in my action, I can use handleRequest() instead, and access all of the model collections somehow - could someone please help me understand how I do that? If I foreach over $request, are the collections in there somewhere? Thanks!

0 likes
5 replies
aurawindsurfing's avatar

Can you show your action code, please?

Action in Nova receives models so it will probably only iterate over the ones that are passed to it unless you overwrite that.

lara128198's avatar

Hi.

I expected to see all of my 758 models to iterate over, but only see the final chunk of 158

public function handle(ActionFields $fields, Collection $models)
{

// dump(count($models)); // return null;

    $data = [];
    foreach($models as $memberResource) {
        $data[] =  $memberResource->id;
    }

    $ids = implode(',', $data);

    return Action::redirect('/print-badge?ids='.$ids);
}

In the attached link, the suggestion was to use this:

public function handleRequest(\Laravel\Nova\Http\Requests\ActionRequest $request)
{
 
}

When I add that, handle() is not called, only handleRequest, which is fine but what to do next to get all the models?

Many thanks!

lara128198's avatar

I've just found this ticket: https://github.com/laravel/nova-issues/issues/326

Near the end, on 10th Jan:

"But I was able to get around this issue by copying the collection on the handle method and performing the intended action on handleRequest. Why it works? I have no idea. I did it by accident."

I'll give this a try, although I initially thought that handle() was no longer being called. What confuses me most, is that handle runs per chunk, so if I am to save the collection, won't my store collection be out of scope with the next action instance, or is it somehow the same instance? That may be a newb question on my part.

lara128198's avatar

Yes, I double-checked and handle() no longer is called. I think I'm close.

private $data = [];

/**
 * 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)
{
    dump(1); // I don't see this 4 times for the chunks (200, 200, 200, 158)
    foreach($models as $memberResource) {
        $this->data[] =  $memberResource->id;
    }

    return null;
}

public function handleRequest(\Laravel\Nova\Http\Requests\ActionRequest $request)
{
    // parent::handleRequest($request);
    $ids = implode(',', $this->data);
    dump($ids); // I just see an empty string.
    return null;
    // return Action::redirect('/print-badge?ids='.$ids);
}
lara128198's avatar

lol. ok, all fixed. I uncommented the critical parent reference :)

1 like

Please or to participate in this conversation.