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

[8.x] Get queueable relationship when collection has non-numeric keys #37556

Merged
merged 2 commits into from
Jun 1, 2021
Merged

[8.x] Get queueable relationship when collection has non-numeric keys #37556

merged 2 commits into from
Jun 1, 2021

Conversation

standaniels
Copy link
Contributor

@standaniels standaniels commented Jun 1, 2021

Given there is a Post model with a has many relationship to Translation models and Translation models have a locale attribute, which is unique for every Post. For easier accessing the related Translation models, the collection of translations could be keyed by the locale attribute, so that getting a Translation model for locale nl is possible by calling $post->translations->get('nl').

This would be the Post model class.

class Post extends Model
{
    public function translations(): HasMany
    {
        return $this->hasMany(Translation::class);
    }
}

And this would be the Translation model class.

class Translation extends Model
{
    public function newCollection(array $models = []): Collection
    {
        return parent::newCollection($models)->keyBy('locale');
    }
}

This is where an issue arises. By dispatching a job which requires an instance of Post, $post->getQueueableRelations() is called by the SerializesAndRestoresModelIdentifiers trait. It then fails with the following errors, as demonstrated in the test in this PR.

On PHP 7.3 and 7.4:

Cannot unpack array with string keys

On PHP 8.0:

array_intersect() does not accept unknown named parameters

A solution would be to change array_intersect(...$relations) to array_intersect(...array_values($relations)), which obviously would drop any keys.

I've applied this change locally and all the tests that I can run without any further configuration (some require additional modules) pass. What do you think?

@driesvints
Copy link
Member

Yeah I think that change makes sense @standaniels.

@driesvints driesvints changed the title Get queueable relationship when collection has non-numeric keys [8.x] Get queueable relationship when collection has non-numeric keys Jun 1, 2021
@taylorotwell taylorotwell merged commit 1173cca into laravel:8.x Jun 1, 2021
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.

3 participants