もがき系プログラマの日常

もがき系エンジニアの勉強したこと、日常のこと、気になっている技術、備忘録などを紹介するブログです。

laravelのpaginateでgroupbyもしたい

はじめに

こんばんは。

今回もlaravelネタです。

paginateしつつデータをgroupbyしたいなってときがあって、それを調べた対応です。

本題

元データはこちらでやってみます。

$paginator = new \Illuminate\Pagination\Paginator(collect([
    ['userId' => 10,  'type' => 1, 'name' => 'AAA'],
    ['userId' => 20,  'type' => 2, 'name' => 'BBB'],
    ['userId' => 30,  'type' => 3, 'name' => 'CCC'],
    ['userId' => 40,  'type' => 1, 'name' => 'DDD'],
    ['userId' => 50,  'type' => 2, 'name' => 'EEE'],
    ['userId' => 60,  'type' => 3, 'name' => 'FFF'],
    ['userId' => 70,  'type' => 1, 'name' => 'GGG'],
    ['userId' => 80,  'type' => 2, 'name' => 'HHH'],
    ['userId' => 90,  'type' => 3, 'name' => 'III'],
    ['userId' => 100, 'type' => 1, 'name' => 'JJJ'],
    ['userId' => 110, 'type' => 2, 'name' => 'KKK'],
    ['userId' => 120, 'type' => 3, 'name' => 'LLL'],
    ['userId' => 130, 'type' => 1, 'name' => 'MMM'],
    ['userId' => 140, 'type' => 2, 'name' => 'NNN'],
    ['userId' => 150, 'type' => 3, 'name' => 'OOO'],
    ['userId' => 160, 'type' => 1, 'name' => 'PPP'],
    ['userId' => 170, 'type' => 2, 'name' => 'QQQ'],
    ['userId' => 180, 'type' => 3, 'name' => 'RRR'],
    ['userId' => 190, 'type' => 1, 'name' => 'SSS'],
    ['userId' => 200, 'type' => 2, 'name' => 'TTT'],
    ['userId' => 210, 'type' => 3, 'name' => 'UUU'],
    ['userId' => 220, 'type' => 1, 'name' => 'VVV'],
    ['userId' => 230, 'type' => 2, 'name' => 'WWW'],
    ['userId' => 240, 'type' => 3, 'name' => 'XXX'],
    ['userId' => 250, 'type' => 1, 'name' => 'YYY'],
    ['userId' => 260, 'type' => 2, 'name' => 'ZZZ'],
]), 5);

通常で取得するとこうなります。

dd($paginator->items());


/**
 * array:5 [▼
 *   0 => array:3 [▼
 *     "userId" => 10
 *     "type" => 1
 *     "name" => "AAA"
 *   ]
 *   1 => array:3 [▼
 *     "userId" => 20
 *     "type" => 2
 *     "name" => "BBB"
 *   ]
 *   2 => array:3 [▼
 *     "userId" => 30
 *     "type" => 3
 *     "name" => "CCC"
 *   ]
 *   3 => array:3 [▼
 *     "userId" => 40
 *     "type" => 1
 *     "name" => "DDD"
 *   ]
 *   4 => array:3 [▼
 *     "userId" => 50
 *     "type" => 2
 *     "name" => "EEE"
 *   ]
 * ]
 */

これをtypeでgroupbyしてみます。

$paginator->setCollection($paginator->groupBy('type'));
dd($paginator->items());


/**
 * array:3 [▼
 *   1 => Illuminate\Support\Collection {#1370 ▼
 *     #items: array:2 [▼
 *       0 => array:3 [▼
 *         "userId" => 10
 *         "type" => 1
 *         "name" => "AAA"
 *       ]
 *       1 => array:3 [▼
 *         "userId" => 40
 *         "type" => 1
 *         "name" => "DDD"
 *       ]
 *     ]
 *     #escapeWhenCastingToString: false
 *   }
 *   2 => Illuminate\Support\Collection {#1388 ▼
 *     #items: array:2 [▼
 *       0 => array:3 [▼
 *         "userId" => 20
 *         "type" => 2
 *         "name" => "BBB"
 *       ]
 *       1 => array:3 [▼
 *         "userId" => 50
 *         "type" => 2
 *         "name" => "EEE"
 *       ]
 *     ]
 *     #escapeWhenCastingToString: false
 *   }
 *   3 => Illuminate\Support\Collection {#1374 ▼
 *     #items: array:1 [▼
 *       0 => array:3 [▼
 *         "userId" => 30
 *         "type" => 3
 *         "name" => "CCC"
 *       ]
 *     ]
 *     #escapeWhenCastingToString: false
 *   }
 * ]
 */

重要なのは setCollectionで内部のitemsを上書きすることみたいです。

    /**
     * Set the paginator's underlying collection.
     *
     * @param  \Illuminate\Support\Collection  $collection
     * @return $this
     */
    public function setCollection(Collection $collection)
    {
        $this->items = $collection;

        return $this;
    }

終わりに

以外に簡単でした。