PHPackages                             hulkur/laravel-hasmany-keyby - 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. [Database &amp; ORM](/categories/database)
4. /
5. hulkur/laravel-hasmany-keyby

ActiveLibrary[Database &amp; ORM](/categories/database)

hulkur/laravel-hasmany-keyby
============================

Add possibility to have related models in many-to-many relationship attached to the parent model by defined key values.

9.0(1mo ago)485.4k↓14.6%2MITPHPPHP ^8.3CI passing

Since Apr 4Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/hulkur/laravel-hasmany-keyby)[ Packagist](https://packagist.org/packages/hulkur/laravel-hasmany-keyby)[ Docs](https://github.com/hulkur/laravel-hasmany-keyby)[ RSS](/packages/hulkur-laravel-hasmany-keyby/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (10)Dependencies (15)Versions (14)Used By (0)

Extend Laravel HasMany relationship with KeyBy method
=====================================================

[](#extend-laravel-hasmany-relationship-with-keyby-method)

[![Latest Version on Packagist](https://camo.githubusercontent.com/04f7b56be842003412efd14cc835bf5d38670a4e0a8b21e9575919e7b2127216/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f68756c6b75722f6c61726176656c2d6861736d616e792d6b657962792e737667)](https://packagist.org/packages/hulkur/laravel-hasmany-keyby)[![Laravel Compatibility](https://camo.githubusercontent.com/a89a3ec881110986de2255b7af1cc99d0950ebafa07d146a3b45f4e25138c03a/68747470733a2f2f62616467652e6c61726176656c2e636c6f75642f62616467652f68756c6b75722f6c61726176656c2d6861736d616e792d6b65796279)](https://packagist.org/packages/hulkur/laravel-hasmany-keyby)[![GitHub Workflow Status](https://camo.githubusercontent.com/5c6b6df4b73df616e6bbe3c7fce23e0421ec69495c3a30fb936d1328207bce79/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f68756c6b75722f6c61726176656c2d6861736d616e792d6b657962792f74657374732e796d6c3f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/5c6b6df4b73df616e6bbe3c7fce23e0421ec69495c3a30fb936d1328207bce79/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f68756c6b75722f6c61726176656c2d6861736d616e792d6b657962792f74657374732e796d6c3f6272616e63683d6d6173746572)[![Total Downloads](https://camo.githubusercontent.com/05567193c1905c123969196fc78370b4908e927ef7004a8c69f94af4c2059af6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f68756c6b75722f6c61726176656c2d6861736d616e792d6b657962792e737667)](https://packagist.org/packages/hulkur/laravel-hasmany-keyby)

Adds possibility to have related models in many-to-many relationship attached to the parent model by defined key values. Usually it would be related model `id`.

call:

```
$users = User::with('groups')->all()
```

laravel default:

```
$user->groups = [0 => $group];
```

New usage with keyBy:

```
class User extends Model
{
  use HasManyKeyByRelationship;

  public function groups()
  {
    return $this->hasMany(Group::class)->keyBy('id'); // can be string or callable
  }
}
```

result:

```
$user->groups = [$group->id => $group];
```

Some real life use cases
------------------------

[](#some-real-life-use-cases)

1. When you need to target order items by product id to apply order discounts.

    - Ex: discount for specific product on order total/shipping method etc
2. When you need to manipulate pivot records in mass.

    - Ex: users/groups grid where grid fields are some value in pivot record and not all pivot records exist

```
@foreach($users as $user)
  @foreach($groups as $group)
    {{$user->accesslevels[$group->id]?->level}}
  @endforeach
@endforeach
```

###  Health Score

60

—

FairBetter than 98% of packages

Maintenance91

Actively maintained with recent releases

Popularity35

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity83

Battle-tested with a long release history

 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 ~215 days

Recently: every ~294 days

Total

13

Last Release

57d ago

Major Versions

4.0.2 → 5.02022-02-08

5.0 → 6.02023-02-17

6.0 → 7.02024-03-25

7.1 → 8.02025-03-02

8.0 → 9.02026-05-08

PHP version history (7 changes)1.0PHP ^7.2

4.0PHP ^7.2|^8.0

4.0.2PHP ^7.3|^8.0

5.0PHP ^8.0

6.0PHP ^8.1

7.0PHP ^8.2

9.0PHP ^8.3

### Community

Maintainers

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

---

Top Contributors

[![hulkur](https://avatars.githubusercontent.com/u/11457732?v=4)](https://github.com/hulkur "hulkur (37 commits)")

---

Tags

laravel

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/hulkur-laravel-hasmany-keyby/health.svg)

```
[![Health](https://phpackages.com/badges/hulkur-laravel-hasmany-keyby/health.svg)](https://phpackages.com/packages/hulkur-laravel-hasmany-keyby)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M96](/packages/mongodb-laravel-mongodb)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)[api-platform/laravel

API Platform support for Laravel

58171.6k14](/packages/api-platform-laravel)[laravel-liberu/laravel-gedcom

A package that converts gedcom files to Eloquent models

782.5k1](/packages/laravel-liberu-laravel-gedcom)

PHPackages © 2026

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