PHPackages                             melihovv/collection-grouped-by-model - 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. melihovv/collection-grouped-by-model

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

melihovv/collection-grouped-by-model
====================================

A collection grouped by model

1.6.0(6y ago)91.5k2MITPHPPHP &gt;=7.2CI failing

Since Oct 22Pushed 6y ago1 watchersCompare

[ Source](https://github.com/melihovv/collection-grouped-by-model)[ Packagist](https://packagist.org/packages/melihovv/collection-grouped-by-model)[ RSS](/packages/melihovv-collection-grouped-by-model/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (6)Dependencies (2)Versions (8)Used By (0)

Collection Grouped By Model
===========================

[](#collection-grouped-by-model)

[![GitHub Workflow Status](https://github.com/melihovv/collection-grouped-by-model/workflows/Run%20tests/badge.svg)](https://github.com/melihovv/collection-grouped-by-model/actions)[![styleci](https://camo.githubusercontent.com/19c13d62c768918233c903ebd5163c981ff4023c3d423f908ad5be3d9c9304c4/68747470733a2f2f7374796c6563692e696f2f7265706f732f3130373832323030312f736869656c64)](https://styleci.io/repos/107822001)

[![Packagist](https://camo.githubusercontent.com/55c1f4ad828fcbc06cfca22368f6ec6f1fcfc5cac5b54b07a3f1221cd5b9aef3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d656c69686f76762f636f6c6c656374696f6e2d67726f757065642d62792d6d6f64656c2e737667)](https://packagist.org/packages/melihovv/collection-grouped-by-model)[![Packagist](https://camo.githubusercontent.com/b5b772d72449f61fdc2262b781bf517d41154f46f01195ecd4acb831258b185d/68747470733a2f2f706f7365722e707567782e6f72672f6d656c69686f76762f636f6c6c656374696f6e2d67726f757065642d62792d6d6f64656c2f642f746f74616c2e737667)](https://packagist.org/packages/melihovv/collection-grouped-by-model)[![Packagist](https://camo.githubusercontent.com/f91d5c25f30c107be02cc0d436961ef5f8f359f5c11082132b492ef297e8684c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d656c69686f76762f636f6c6c656374696f6e2d67726f757065642d62792d6d6f64656c2e737667)](https://packagist.org/packages/melihovv/collection-grouped-by-model)

This package allows easily group laravel collection by any php object (model, array/collection of models, etc), not only by scalars (strings, numbers, booleans).

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

[](#installation)

Install via composer

```
composer require melihovv/collection-grouped-by-model

```

Usage
-----

[](#usage)

```
$posts = Post::all()
$postsGroupedByAuthor = (new CollectionGroupedByModel($posts))->groupByModel('author_id', 'author');

foreach ($postsGroupedByAuthor as $authorPosts) {
  $authorPosts->model(); // returns posts' author
  $authorPosts->collection(); // returns author's posts
}
```

### Nested grouping: first group products by category, then by manufacturer.

[](#nested-grouping-first-group-products-by-category-then-by-manufacturer)

```
$products = Product::all();
$groupedProducts = (new CollectionGroupedByModel($products))
    ->groupByModel('category_id', 'category')
    ->transform(function (CollectionGroupedByModel $productsGroupedByCategory) {
      return $productsGroupedByCategory->groupByModel('manufacturer_id', 'manufacturer');
    });

foreach ($groupedProducts as $categoryProducts) {
  $categoryProducts->model(); // returns category

  foreach ($categoryProducts->collection() as $manufacturerProducts) {
    $manufacturerProducts->model(); // returns manufacturer
    $manufacturerProducts->collection(); // returns products grouped by category and manufacturer
  }
}
```

### Group by several models

[](#group-by-several-models)

```
$posts = Post::all()
$postsGroupedByAuthorAndCategory = (new CollectionGroupedByModel($posts))
    ->groupByModel(function (Post $post) {
      return "$post->author_id,$post->category_id";
    }, function (Post $post) {
      return [$post->author, $post->category];
    });

foreach ($postsGroupedByAuthorAndCategory as $authorPostsInCategory) {
  list($author, $category) = $authorPostsWithCategory->model();
  // or using php 7.1 array destruction
  [$author, $category] = $authorPostsWithCategory->model();
  $authorPostsWithCategory->collection(); // returns author's posts in category
}
```

Security
--------

[](#security)

If you discover any security related issues, please email instead of using the issue tracker.

Credits
-------

[](#credits)

- [Alexander Melihov](https://github.com/melihovv/collection-grouped-by-model)
- [All contributors](https://github.com/melihovv/collection-grouped-by-model/graphs/contributors)

This package is bootstrapped with the help of [melihovv/laravel-package-generator](https://github.com/melihovv/laravel-package-generator).

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity63

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

Recently: every ~189 days

Total

7

Last Release

2353d ago

PHP version history (2 changes)1.0.0PHP &gt;=7.0

1.5.0PHP &gt;=7.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8608721?v=4)[Alexander Melihov](/maintainers/melihovv)[@melihovv](https://github.com/melihovv)

---

Top Contributors

[![melihovv](https://avatars.githubusercontent.com/u/8608721?v=4)](https://github.com/melihovv "melihovv (27 commits)")

---

Tags

collectionlaravellaravel-packagelaravelcollectiongrouped

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/melihovv-collection-grouped-by-model/health.svg)

```
[![Health](https://phpackages.com/badges/melihovv-collection-grouped-by-model/health.svg)](https://phpackages.com/packages/melihovv-collection-grouped-by-model)
```

###  Alternatives

[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M11](/packages/renatomarinho-laravel-page-speed)[emargareten/inertia-modal

Inertia Modal is a Laravel package that lets you implement backend-driven modal dialogs for Inertia apps.

90157.6k](/packages/emargareten-inertia-modal)[forjedio/inertia-table

Backend-driven dynamic tables for Laravel + Inertia.js

272.0k](/packages/forjedio-inertia-table)[tomshaw/electricgrid

A feature-rich Livewire package designed for projects that require dynamic, interactive data tables.

119.8k](/packages/tomshaw-electricgrid)

PHPackages © 2026

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