Git Product home page Git Product logo

collection-grouped-by-model's Introduction

Collection Grouped By Model

Build Status styleci Scrutinizer Code Quality SensioLabsInsight

Packagist Packagist Packagist

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

Install via composer

composer require melihovv/collection-grouped-by-model

Usage

$posts = Post::all()
$postsGroupedByAuthor = (new CollectionGroupedByModel($posts))
    ->groupByModel(function (Post $post) {
      return $post->author_id;
    }, function (Post $post) {
      return $post->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(function (Product $product) {
      return $product->category_id;
    }, function (Product $product) {
      return $product->category;
    })
    ->transform(function (CollectionGroupedByModel $productsGroupedByCategory) {
      return $productsGroupedByCategory
        ->groupByModel(function (Product $product) {
          return $product->manufacturer_id;
        }, function (Product $product) {
          return $product->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 [email protected] instead of using the issue tracker.

Credits

This package is bootstrapped with the help of melihovv/laravel-package-generator.

collection-grouped-by-model's People

Contributors

melihovv avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.