PHPackages                             rtablada/eloquent-rankable - 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. rtablada/eloquent-rankable

ActiveLibrary

rtablada/eloquent-rankable
==========================

A Rankable Eloquent Model Package

31432PHP

Since Aug 2Pushed 12y ago1 watchersCompare

[ Source](https://github.com/rtablada/eloquentRankable)[ Packagist](https://packagist.org/packages/rtablada/eloquent-rankable)[ RSS](/packages/rtablada-eloquent-rankable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Eloquent Rankable
=================

[](#eloquent-rankable)

This package makes rankable models for sorting quick and easy.

Setting up a Rankable Model
---------------------------

[](#setting-up-a-rankable-model)

Making Rankable Models is just as easy as creating regular Eloquent Models with just one more property `protected $metricsWeight`!

An example model would look something like this:

```
use Rtablada\EloquentRankable\RankableModel;

class Friend extends RankableModel
{
	protected $metricWeights = array(
		'search' => 0.8,
		'name' => 0.2
	);

	protected $fillable = array('name', 'rank');
}
```

In your Schema remember to include a `rank` column (I suggest using a Decimal fieldtype with a 10 digits and 4 decimals).

The `$metricWeight` Property
----------------------------

[](#the-metricweight-property)

The `$metricWeight` property is an easy way to modify the ranking property of your models. You can set weights for whenever you use an `updateMetric*` function.

So if you want to update a model's rank when you get a result in a search you could run `$model->updateMetricSearch()` which will raise the ranking by 0.8 points.

These `updateMetric` functions can also be used in mutators or accessors.

```
public function setNameAttribute($value)
{
	$this->attributes['name'] = $value;
	$this->updateMetricName();
}
```

Rank Queries
------------

[](#rank-queries)

Any time you want to get results already sorted descending by `rank` you can just prepend your wanted query builder function with `rank`. For example:

```
$friends = Friend::rankAll();
$friendsPaginated = Friend::rankPaginate();
```

Updating Rank In Comparison To Other Entries
--------------------------------------------

[](#updating-rank-in-comparison-to-other-entries)

The model also gives you the ability to `rankBefore`, `rankBetween`, or `rankAfter` another model instance.

```
$friendLow = Friend::find(1);
$friendHigh = Friend::find(1);

$friendLow->rankBefore($friendHigh);
```

Updating All Entries With Sorted `ids`
--------------------------------------

[](#updating-all-entries-with-sorted-ids)

For uses such as Javascript Web Apps, Rankable gives you a quick and easy way to update the rankings between entries.

```
$desiredIds = array(1,2,3);
$friends = Friend::rankOrderSet($desiredIds);
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/ea9ccd400c74151601341e320935a31d1a9779c5eb49a8f364bc7a0ac75baa32?d=identicon)[rtablada](/maintainers/rtablada)

---

Top Contributors

[![rtablada](https://avatars.githubusercontent.com/u/2532004?v=4)](https://github.com/rtablada "rtablada (15 commits)")

### Embed Badge

![Health badge](/badges/rtablada-eloquent-rankable/health.svg)

```
[![Health](https://phpackages.com/badges/rtablada-eloquent-rankable/health.svg)](https://phpackages.com/packages/rtablada-eloquent-rankable)
```

PHPackages © 2026

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