2017 © Pedro Peláez
 

library collection-grouped-by-model

A collection grouped by model

image

melihovv/collection-grouped-by-model

A collection grouped by model

  • Sunday, February 11, 2018
  • by melihovv
  • Repository
  • 2 Watchers
  • 10 Stars
  • 824 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Collection Grouped By Model

GitHub Workflow Status styleci, (*1)

Packagist Packagist Packagist, (*2)

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

Installation

Install via composer, (*4)

composer require melihovv/collection-grouped-by-model

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.

$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

$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

If you discover any security related issues, please email amelihovv@ya.ru instead of using the issue tracker., (*5)

Credits

This package is bootstrapped with the help of melihovv/laravel-package-generator., (*6)

The Versions

11/02 2018

dev-master

9999999-dev

A collection grouped by model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alexander Melihov

laravel collection grouped

10/02 2018

1.2.0

1.2.0.0

A collection grouped by model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alexander Melihov

laravel collection grouped

20/01 2018

1.1.0

1.1.0.0

A collection grouped by model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alexander Melihov

laravel collection grouped

22/10 2017

1.0.0

1.0.0.0

A collection grouped by model

  Sources   Download

MIT

The Requires

 

The Development Requires

by Alexander Melihov

laravel collection grouped