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] Support times() with raw() #34667

Merged
merged 1 commit into from
Oct 5, 2020
Merged

Conversation

dwightwatson
Copy link
Contributor

With legacy model factories it was possible to get an array of raw attributes.

$posts = factory(Post::class, 10)->raw();

$posts->count() === 10;

However, the raw method I added in an earlier PR doesn't respect the times() method. Calling it made no effect on the returned result.

This PR now respects times() and returns an array of raw attributes when set.

// Before this PR:
Post::factory()->times(10)->raw(); // [...raw attributes of post]

// Following this PR:
Post::factory()->times(10)->raw(); // [array of 10 arrays with raw attributes of post]


return array_map(function () use ($attributes, $parent) {
return $this->state($attributes)->getExpandedAttributes($parent);
}, range(1, $this->count));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Null Coalescing Operator instead of checking for null

range(1, $this->count ?? 1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because that would return an array containing an array of raw attributes. If the count is null you just want the raw attributes themselves. This code uses the same pattern from the make method.

@taylorotwell taylorotwell merged commit 4a6d281 into laravel:8.x Oct 5, 2020
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