tunezilla/nova-dynamic-action-fields

[Question] Why ignoring 'forAllMatchingResources()`?

Closed this issue · 4 comments

HI there,

just wondering if there is a special case where it would make sense to ignore ActionRequests that do have selected the resources by using Select Matching (s. related code)?

Hey, I'm not sure why that check is performed - would need a time machine 😅

If this breaking something for you, do you have an example flow where ?resources=all is sent to an action? Our use of this package has been for single resources only, so I don't think this is something we run in to.

Thanks!

Thanks for your fast response!

Yes, we have several actions where the user sometimes selects 'all matching' which resulted into missing fields for those actions. We currently added our own DynamicFieldAction Trait which uses yours under the hood and overwrites the fields() method to get around the described issue.

use Laravel\Nova\Http\Requests\ActionRequest;
use TuneZilla\DynamicActionFields\DynamicFieldAction as BaseDynamicFieldAction;

trait DynamicFieldAction
{
    use BaseDynamicFieldAction;

    public function fields()
    {
        /** @var ActionRequest $actionRequest */
        $actionRequest = $this->actionRequest ?? app(ActionRequest::class);

        $models = [];

        if ($actionRequest->has('resources')) {
            $actionRequest->chunks(100, function ($chunkedModels) use (&$models) {
                /** @var \Illuminate\Database\Eloquent\Collection $chunkedModels */
                $models = array_merge($models, $chunkedModels->all());
            });
        }

        return $this->fieldsForModels(collect($models));
    }
}

Thanks a lot @tunezilla!