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] Use a map to prevent unnecessary array access #32296

Merged
merged 1 commit into from
Apr 8, 2020
Merged

[6.x] Use a map to prevent unnecessary array access #32296

merged 1 commit into from
Apr 8, 2020

Conversation

jasonvarga
Copy link
Contributor

I'm sure I'll get the "resources are an eloquent feature" response, but it's so great for us to use without needing to reinvent any wheels, and this is a tiny change so I thought it's worth a shot.

This change introduced a while back broke paginated API resources where people weren't using Eloquent models.

There are a bunch of issues #29858, #29916, #31703, #31704 (probably others) related to it.

This PR fixes the root cause for most of them. By changing from pluck to map, we avoid an array access call to the Eloquent model (or non-Eloquent model in our cases) that happens further down the stack trace. It'll let those of us who still want to use Resources to avoid ugly workarounds.

@taylorotwell taylorotwell merged commit 0347c51 into laravel:6.x Apr 8, 2020
@GrahamCampbell GrahamCampbell changed the title Use a map to prevent unnecessary array access [6.x] Use a map to prevent unnecessary array access Apr 8, 2020
@jasonvarga
Copy link
Contributor Author

Thanks! 🎉

jasonvarga added a commit to statamic/cms that referenced this pull request Apr 8, 2020
Originally we added arrayaccess onto entries etc to get it to work. That broke other stuff. Removed in 4903044e. This is the alternative.

This can be removed if/when laravel/framework#32296 is merged and tagged.
@driesvints
Copy link
Member

@jasonvarga I guess this is an acceptable workaround, thanks for the pr.

But like you said I again want to stress that resources are designed to work with Eloquent models so you could still run into problems at some point.

@jasonvarga
Copy link
Contributor Author

I'm fine with taking that risk. The alternative would be to make our own nearly identical implementation. 😬

@jasonvarga jasonvarga deleted the avoid-arrayaccess-in-resources branch April 9, 2020 17:23
@mholmes-hs
Copy link

mholmes-hs commented Apr 22, 2020

Seems to break my code

        $query = Pet::where('shelter_id', $shelter->id);
        if($searchValue) {
            $query = $query->where('shelter_reference', 'like', "$searchValue%");
        }
        if($orderBy) {
         $query = $query->orderBy($orderBy, $orderByDir);
        }
        $data = $query->paginate($length);

        $data
            ->getCollection()
            ->transform(function($pet) {
                return [
                    'uuid' => $pet->uuid,
                    'shelter_reference' => "#$pet->shelter_reference",
                    'dueout_date' => $pet->dueout_date
                        ? $pet->dueout_date->format('m/d/Y')
                        : null,
                    'intake_date' => $pet->intake_date->format('m/d/Y'),
                    'is_published' => $pet->is_published ? 'Published' : 'Draft',
                    'amount_raised' => "$$pet->amount_raised",
                    'id' => $pet->id,
                    'type' => $pet->type,
                ];
            }); ```

@Shhu
Copy link
Contributor

Shhu commented Apr 23, 2020

It break my code too with a kinda misleading error
Trying to get property 'resource' of non-object'

Had to replace
'data' => $this->collection->transform(static function (User $user) {
with
'data' => $this->collectResource($this->collection)->transform(static function (User $user) {

@driesvints
Copy link
Member

@mholmes-hs @Shhu can one of you two please open up an issue and add full steps to reproduce? Please try to keep the code as minimal as possible to reproduce.

@Shhu
Copy link
Contributor

Shhu commented Apr 23, 2020

@driesvints done with #32513

@edwink75
Copy link

edwink75 commented Feb 4, 2021

It break my code too with a kinda misleading error
Trying to get property 'resource' of non-object'

Had to replace
'data' => $this->collection->transform(static function (User $user) {
with
'data' => $this->collectResource($this->collection)->transform(static function (User $user) {

This works like a charm, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants