samedi 24 août 2019

How to group my results as nested array after join table with Laravel Eloquent

On my situation, i have two tables (collections, products) with having 1 to many relationship.

With code example, it is :

$collectionProducts = collections::join('products', 'products.collection_id', '=', 'collections.collection_id');

And i would like to make the result like:

[
  Collection 1 => [
    Product 1 => [
        ...Product Info
    ],
    Product 2 => [
        ...Product Info
    ],
  ],
  Collection 2 => [
    Product 1 => [
        ...Product Info
    ],
    Product 2 => [
        ...Product Info
    ],
  ]
]

How can i achieve my expected results ? I know this is the common usage, i am sorry if there is any duplicated questions asked before, because i just cannot found any issues matched.

Thanks a lot.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire