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. hulkur/laravel-hasmany-keyby

ActiveLibrary

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

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

8.0(1y ago)483.6k↑82.5%1MITPHPPHP ^8.2CI passing

Since Apr 4Pushed 1y 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 1mo ago

READMEChangelog (10)Dependencies (8)Versions (13)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)[![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

46

—

FairBetter than 93% of packages

Maintenance44

Moderate activity, may be stable

Popularity35

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity79

Established project with proven stability

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

Recently: every ~279 days

Total

12

Last Release

442d ago

Major Versions

3.0 → 4.02021-01-20

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

PHP version history (6 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

### 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 (35 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

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[fumeapp/modeltyper

Generate TypeScript interfaces from Laravel Models

196277.9k](/packages/fumeapp-modeltyper)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

44643.1k1](/packages/pressbooks-pressbooks)[api-platform/laravel

API Platform support for Laravel

59126.4k6](/packages/api-platform-laravel)[dragon-code/migrate-db

Easy data transfer from one database to another

15717.4k](/packages/dragon-code-migrate-db)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

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