Create Groups and Group Items Within Them

Asked

Viewed 141 times

2

I have the following problem, I have a table with items and I need to create groups and name each item to its own group. I have already distributed them by categories, which in this case I call 'families', but I now need to group them respectively into groups that are generated automatically that I can call a maximum value.

Essa é a tabela:

Código

this is the code that returns me the table above, but I needed it to return separately this way here:

Assim:

  • Welcome to Stackoverflow Gabriel. Have you made any attempt at code to accomplish the task you want? I suggest you read this help article from the site: How to create a Minimum, Complete and Verifiable example.

  • Which groups? Do you have examples of how the answer would be? missed even finishing the question, with no context to finish.

  • Yes, actually I want to distribute items that I have in stock that are separated by categories and distribute in these groups that are called pallets, ex: pallet1, pallet2 etc in automatic.

  • Okay, but, how would this breakup?

1 answer

1

You can play the result in a collecion, then use the groupBy function().

$collection = collect([
    ['account_id' => 'account-x10', 'product' => 'Chair'],
    ['account_id' => 'account-x10', 'product' => 'Bookcase'],
    ['account_id' => 'account-x11', 'product' => 'Desk'],
]);

$grouped = $collection->groupBy('account_id');

$grouped->toArray();

/*
    [
        'account-x10' => [
            ['account_id' => 'account-x10', 'product' => 'Chair'],
            ['account_id' => 'account-x10', 'product' => 'Bookcase'],
        ],
        'account-x11' => [
            ['account_id' => 'account-x11', 'product' => 'Desk'],
        ],
    ]
*/

This way you will have a list within a list. And in the view will have two tables (subtable). As you didn’t post your view, nor the result of the query there is no way I show the example taking your data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.