Skip to content

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

Merged
taylorotwell merged 1 commit into
laravel:10.xfrom
WendellAdriel:percentage-high-order
Oct 10, 2023
Merged

taylorotwell merged 1 commit into
laravel:10.xfrom
WendellAdriel:percentage-high-order

Conversation

@WendellAdriel

@WendellAdriel WendellAdriel commented Oct 10, 2023 •

Copy link
Copy Markdown
Member

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