Skip to content

[10.x] Add percentage to be used as High Order Messages #48689

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

Merged
merged 1 commit into from
Oct 10, 2023

Conversation

WendellAdriel
Copy link
Contributor

@WendellAdriel WendellAdriel commented Oct 10, 2023

Changes

This PR adds the ability to use the percentage method in the Collections as High Order Messages.

Why

Sometimes when we need to calculate percentages based on some property, instead of doing:

$collection = new $collection([
    ['name' => 'Taylor', 'active' => true],
    ['name' => 'Nuno', 'active' => true],
    ['name' => 'Dries', 'active' => false],
    ['name' => 'Jess', 'active' => true],
]);

$this->assertSame(75.00, $collection->percentage(fn ($value) => $value['active']));

We can simply do:

$collection = new $collection([
    ['name' => 'Taylor', 'active' => true],
    ['name' => 'Nuno', 'active' => true],
    ['name' => 'Dries', 'active' => false],
    ['name' => 'Jess', 'active' => true],
]);

$this->assertSame(75.00, $collection->percentage->active);

It's a simple life improvement that if needed many times can save some time and effort

@taylorotwell taylorotwell merged commit 219a497 into laravel:10.x Oct 10, 2023
@WendellAdriel WendellAdriel deleted the percentage-high-order branch October 10, 2023 13:10
timacdonald pushed a commit to timacdonald/framework that referenced this pull request Oct 24, 2023
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.

2 participants