PHPackages                             codewithkyrian/laravel\_position - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. codewithkyrian/laravel\_position

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

codewithkyrian/laravel\_position
================================

A simple extensible laravel collection macro that evaluates the position or ranking of items in a collection and appends the position to each item with a key of your choice.

1.0.1(4y ago)017MITPHPPHP &gt;=7.4

Since Jan 11Pushed 4y ago1 watchersCompare

[ Source](https://github.com/CodeWithKyrian/laravel-position)[ Packagist](https://packagist.org/packages/codewithkyrian/laravel_position)[ Docs](https://github.com/codewithkyrian/laravel-position)[ RSS](/packages/codewithkyrian-laravel-position/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)DependenciesVersions (4)Used By (0)

Laravel Position
================

[](#laravel-position)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5fe3cabaf8caf18a10db86cb681b40c6de1495f7e8e8c7f0aa79dd6c14e1ae11/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f6465776974686b797269616e2f6c61726176656c5f706f736974696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/codewithkyrian/laravel_position)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/d5c2b284b8d2efad6624cf785611af46a642795ed8467a11eb857fad628b9dbd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f636f6465776974686b797269616e2f6c61726176656c2d706f736974696f6e2f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/codewithkyrian/laravel-position/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/8024e2df37ada9145aef0c66b65d79cd92c97ef1b79c6a101a66492825e5529f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f6465776974686b797269616e2f6c61726176656c5f706f736974696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/codewithkyrian/laravel_position)

A simple extensible laravel collection macro that evaluates the position or ranking of items in a collection and appends the position to each item with a key of your choice. The default key is 'position'

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require codewithkyrian/laravel_position
```

You can publish the config file with:

```
php artisan vendor:publish --tag="position-config"
```

This is the contents of the published config file:

```
return [
    /**
     * The default key to use when adding the position to each item
     * in the collection. It could still be overridden when calling
     * method by passing a string as the second argument.
     */
    'key' => 'position',

    /**
     * Whether to show the total number of items in the collection
     * in the position text eg. 12th out of 30
     */
    'show_total' => false,

    /**
     * The text to join the position and the total number of items
     * in the collection
     */
    'join_text' => 'out of'
];
```

And you're done. You can now use the macro anywhere in your project.

Usage
-----

[](#usage)

To use the macro, you have to call the method `rankBy` on the collection instance while passing the key to be used for the ranking as the first arguement. This package can be used on a collection whether the items in it are arrays or objects.

```
$items = collect(
    [
        [ 'name' => 'Mr A', 'score' => 100 ],
        [ 'name' => 'Mr B', 'score' => 74 ],
        [ 'name' => 'Mr C', 'score' => 60 ],
        [ 'name' => 'Mr D', 'score' => 83 ],
        [ 'name' => 'Mr E', 'score' => 89 ],
    ]
);
$rankedItems = $items->rankBy('score');
dd($rankedItems->toArray());

/*
array:5 [▼
  0 => array:3 [▼
    "name" => "Mr A"
    "score" => 100
    "position" => "1st"
  ]
  1 => array:3 [▼
    "name" => "Mr E"
    "score" => 89
    "position" => "3rd"
  ]
  2 => array:3 [▼
    "name" => "Mr D"
    "score" => 83
    "position" => "4th"
  ]
  3 => array:3 [▼
    "name" => "Mr B"
    "score" => 74
    "position" => "5th"
  ]
  4 => array:3 [▼
    "name" => "Mr C"
    "score" => 60
    "position" => "6th"
  ]
]
*/
```

You can also pass a composite key in the dot notation form for collection with nested values.

```
$users = collect(
    [
        [
            'name' => 'Mr A',
            'result' => [
                'score' => 100, 'status' => 1
            ]
        ],
        [
            'name' => 'Mr B',
            'result' => [
                'score' => 74, 'status' => 1
            ]
        ],
        [
            'name' => 'Mr C',
            'result' => [
                'score' => 60, 'status' => 1
            ]
        ],
        [
            'name' => 'Mr D',
            'result' => [
                'score' => 83, 'status' => 1
            ]
        ]
    ]
);
$rankedUsers = $users->rankBy('result.score');
echo ($rankedUsers->toArray());

/*
array:4 [▼
  0 => array:3 [▼
    "name" => "Mr A"
    "result" => array:2 [▼
      "score" => 100
      "status" => 1
    ]
    "position" => "1st"
  ]
  1 => array:3 [▼
    "name" => "Mr D"
    "result" => array:2 [▼
      "score" => 83
      "status" => 1
    ]
    "position" => "3rd"
  ]
  2 => array:3 [▼
    "name" => "Mr B"
    "result" => array:2 [▼
      "score" => 74
      "status" => 1
    ]
    "position" => "4th"
  ]
  3 => array:3 [▼
    "name" => "Mr C"
    "result" => array:2 [▼
      "score" => 60
      "status" => 1
    ]
    "position" => "5th"
  ]
]
*/
```

To further customize the behaviour of the macro, you could pass a second argument to control the key used to output the position on the items. By default, it is `position`. Remember, you can also change this from the `key` key in the package's config file.

```
$rankedItems = $items->rankBy('score', 'class_position');
echo ($rankedItems->toArray());

/*
array:5 [▼
  0 => array:3 [▼
    "name" => "Mr A"
    "score" => 100
    "class_position" => "1st"
  ]
  1 => array:3 [▼
    "name" => "Mr E"
    "score" => 89
    "class_position" => "3rd"
  ]
  2 => array:3 [▼
    "name" => "Mr D"
    "score" => 83
    "class_position" => "4th"
  ]
  3 => array:3 [▼
    "name" => "Mr B"
    "score" => 74
    "class_position" => "5th"
  ]
  4 => array:3 [▼
    "name" => "Mr C"
    "score" => 60
    "class_position" => "6th"
  ]
]
*/
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Kyrian Obikwelu](https://github.com/CodeWithKyrian)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

1586d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/146c2eb02350558d165083e0018f5377f15f0e71493439c93ee60e7c7ac97fc1?d=identicon)[CodeWithKyrian](/maintainers/CodeWithKyrian)

---

Top Contributors

[![CodeWithKyrian](https://avatars.githubusercontent.com/u/48791154?v=4)](https://github.com/CodeWithKyrian "CodeWithKyrian (6 commits)")

---

Tags

laravelCodeWithKyrianlaravel-position

### Embed Badge

![Health badge](/badges/codewithkyrian-laravel-position/health.svg)

```
[![Health](https://phpackages.com/badges/codewithkyrian-laravel-position/health.svg)](https://phpackages.com/packages/codewithkyrian-laravel-position)
```

###  Alternatives

[highideas/laravel-users-online

This package will provide an online users management.

203113.2k1](/packages/highideas-laravel-users-online)[stephenjude/filament-blog

Filament Blog Builder

20317.8k](/packages/stephenjude-filament-blog)[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13704.6k2](/packages/ralphjsmit-laravel-helpers)[datomatic/nova-detached-actions

A Laravel Nova tool to allow for placing actions in the Nova toolbar detached from the checkbox selection mechanism.

11229.2k](/packages/datomatic-nova-detached-actions)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
