Salem loress
Salem loress

Reputation: 359

Laravel : fetch grouped data to bootstrap table

Hello please i have a collection grouped as multidimensional arrays with keys for example :

"book1" {
    {
        "id" : "1",
        "group" : "book1",
        "name" : "Book X",
        "buy" : "140",
        "test" : "test 1",
    } ,
    {
        "id" : "2",
        "group" : "book1",
        "name" : "Book y",
        "buy" : "1200",
        "test" : "test 2",
    } ,
    {
        "id" : "3",
        "group" : "book1",
        "name" : "Book Z",
        "buy" : "1330",
        "test" : "344",
    }
 },
    "book2" {
 {
        "id" : "6",
        "group" : "book2",
        "name" : "Book N",
        "buy" : "1220",
        "test" : "233",
    }
    ....
 } 

i want to make a table with spliting rows as the first column has keys (books) and other columns has splited rows for sub array dynamically, for example : (this a html code)

<table>
    <tr>
        <th>categorie</th>
        <th>Name</th>
        <th>Buy</th>
    </tr>
    <tr>
        <td>Book 1</td>
        <td>Book AA</td>
        <td>14</td>
    </tr>
    <tr>
        <td rowspan="2">Book 2</td>
        <td>Book BA</td>
        <td>12</td>
    </tr>
    <tr>
        <td>Book BB</td>
        <td>14</td>
    </tr>
    <tr>
        <td rowspan="2">Book 3</td>
        <td>Book CA</td>
        <td>22</td>
    </tr>
    <tr>
        <td>Book CB</td>
        <td>§§</td>
    </tr>
    <tr>
        <td rowspan="3">Book 4</td>
        <td>Book DA</td>
        <td>12</td>
    </tr>
    <tr>
        <td>Book DB</td>
        <td>122</td>
    </tr>
    <tr>
        <td>Book DC</td>
        <td>11</td>
    </tr>
</table>

Could you help to make this html example to dynamic table depend of collection and sub arrays ... And thank you !

Upvotes: 0

Views: 747

Answers (2)

Mohammad
Mohammad

Reputation: 660

maybe you can use laravel collection and GroupBy method, like this

$books = Books::all();

$books->groupby('group');

ex :

$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'],
        ],
    ]
*/

and in your blade like this

<table>
<thead>
  <tr>
    <th>categorie</th>
    <th>Name</th>
    <th>Buy</th>
  </tr>
</thead>
<tbody>
@foreach($grouped as $key => $items)
    @foreach(array_chunk($items, 2) as $chunk)
    <tr>
    @foreach($chunk as $index => $item)
        <td @if($index == 0) rowspan="2" @endif>$key</td>
        <td>$book->name</td>
        <td>$book->buy</td>
    @endforeach
    </tr>
    @endforeach
@endforeach
</tbody>
</table> 

Upvotes: 1

Salem loress
Salem loress

Reputation: 359

i fix it by using this code :

<table class="table table-striped table-inverse table-responsive">
            <thead>
              <tr>
                <th>Book</th>
                <th>Name</th>
                <th>Category</th>
                <th>Buy</th>
              </tr>
            </thead>
            <tbody>
            @foreach($collection as $key => $items)
                @foreach($items->books->groupBy('categorie') as $book)

                    @foreach ($book as $index => $item)
                    @if ($index == "0")
                    <tr >
                        <td class="align-middle" rowspan="{{count($book)}}">{{$item->book->category}}</td>
                        <td >{{$item->name}}</td>
                        <td>{{$item->buy}}</td>
                        <td>{{$item->book}}</td>
                        <td class="align-middle text-center" rowspan="{{count($book)}}">16</td>
                    </tr>
                    @else
                    <tr >
                        <td >{{$item->name}}</td>
                        <td>{{$item->buy}}</td>
                        <td>{{$item->book}}</td>
                    </tr>
                    @endif



                @endforeach
                @endforeach
            @endforeach
            </tbody>
            </table>

And thank for each try to help me

Upvotes: 1

Related Questions