Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Allow passing an array to resource::collection() #30800

Merged
merged 1 commit into from Dec 11, 2019

Conversation

@SjorsO
Copy link
Contributor

SjorsO commented Dec 10, 2019

This PR is similar to #25213. I'm trying to use a resource for an array of "normal" classes/objects:

public function index()
{
    /** @var SomeClass[] $array */
    $array = $this->getClasses();

    return SomeResource::collection($array);
}

This currently doesn't work, and throws the following exception: Call to a member function first() on array at /Illuminate/Http/Resources/CollectsResources.php:24.

For now, I'm wrapping the array in a collection, and then passing it to the resource:

return SomeResource::collection(collect($array));

With this PR, I can just pass the array directly.

@taylorotwell taylorotwell merged commit f799c41 into laravel:6.x Dec 11, 2019
2 checks passed
2 checks passed
continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.