Skip to content

"HigherOrderCollectionProxy as array" error when using withMapValuesToSelectionOptions() #86

Description

@ts-alexey

Describe the bug

An error occurs when using the withMapValuesToSelectionOptions method to map collection values. The package attempts to access a collection method as a property, resulting in a HigherOrderCollectionProxy instead of retrieving the first item.

Steps to Reproduce

Using the following configuration on a field:

->withMapValuesToSelectionOptions(function ($models) {
    return $models->map(function ($model) {
        return [
            'id' => $model->id,
            'label' => $model->name,
        ];
    });
})

Expected Behavior

The options should be mapped successfully without throwing an error.

Actual Behavior / Stack Trace

The following error is thrown:

{
    "class": "Error",
    "message": "Cannot use object of type Illuminate\\Support\\HigherOrderCollectionProxy as array",
    "code": 0,
    "file": "/var/www/inb/vendor/ziffmedia/nova-select-plus/src/SelectPlus.php:372",
    "trace": [
        "/var/www/inb/vendor/ziffmedia/nova-select-plus/src/SelectPlus.php:205"
    ]
}

Proposed Solution

The issue occurs in SelectPlus.php on line 372 because $options->first is being called as a property instead of a method, which returns a HigherOrderCollectionProxy.

Current code:

$first = $options->first; // Returns HigherOrderCollectionProxy
if (!isset($first['label'])) { // Throws Error

Suggested fix:
Change it to call first() as a method:

$first = $options->first(); 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions