0

I have data that I need to display in 3 rows with header in Laravel blade view.I know there is chunk method in Laravel to chunk the data and display.But I want to diplay data in different way.

Here is my data

Illuminate\Support\Collection {#1898 ▼
#items: array:2 [▼
  0 => {#1021 ▼
  +"parameter": "NH3 Examine"
  +"id": 6
  +"pond_quality_id": 5
  +"parameter_id": 28
  +"pond_water_cig": 12
  +"pond_water_non_cig": 12
  +"taken_actions_cig": 12
  +"taken_actions_non_cig": 12
  +"taken_actions": "action"
  +"measure_before_intervention": "before"
  +"measure_after_intervention": "after"
  +"remarks": "remarks1"
  +"created_at": "2021-04-25 10:16:00"
  +"updated_at": "2021-04-25 10:16:00"
}
1 => {#1897 ▼
  +"parameter": "O2 Examine"
  +"id": 7
  +"pond_quality_id": 5
  +"parameter_id": 29
  +"pond_water_cig": 23
  +"pond_water_non_cig": 23
  +"taken_actions_cig": 23
  +"taken_actions_non_cig": 23
  +"taken_actions": "action taken"
  +"measure_before_intervention": "before intervention"
  +"measure_after_intervention": "after intervention"
  +"remarks": "remarks2"
  +"created_at": "2021-04-25 10:16:00"
  +"updated_at": "2021-04-25 10:16:00"
}
]
}

Here is how I want to output my data

col1 col2 col3 col4
col1 val1 col2 val1 col3 val3 col4 val4
col1 val2 col2 val2 col3 val3 col4 val4
col5 col6 col7 col8
col5 val1 col6 val1 col7 val3 col8 val4
col5 val2 col6 val2 col7 val3 col8 val4
col9
col9 val1
col9 val2

How can I achieve this?

simpson
  • 67
  • 8

2 Answers2

0

You have $loop in scope for every @foreach loop you use in blade, that also scales with loops inside of loops

Here's an example of how you could achieve what you want; every 3rd row, add a header of your choice. You might need to play with the modulus there - I'm sure 3 is correct, but considering $loop->iteration starts at 1, not 0, I could be wrong.

Please see: https://laravel.com/docs/8.x/blade for more usages of $loop variable within the context of @foreach

@foreach($items as $item)

    @if($loop->iteration % 3 == 0)
        //output header row
    @endif

    //output $item

@endforeach

You need to define yourself a set of rules for your header rows, considering you have 3 identical tables, but your fourth seems to have 1 header and is unique, you may want to create a helper function that allows you to include a view dependant on the iteration using more than one @if statement in your blade.

Ballard
  • 869
  • 11
  • 25
0

Better way to achieve the expected result is chunks function. Simple and clean.

    @foreach($items->chunk(2) as $chunk)
      <div class="s-table-container">
        <table class="s-table">
          <thead>
            <tr>
              <th>Id</th>
              <th>parameter</th>
              <th>pond_quality_id</th>
              <th>pond_water_cig</th>
             </tr>
           </thead>
           <tbody>
             @foreach($chunk as $item)
               <tr>
                 <td>{{$item->id}}</td>
                 <td>{{$item->parameter}}</td>
                 <td>{{$item->pond_quality_id}}</td>
                 <td>{{$item->pond_water_cig}}</td>
               </tr>
             @endforeach
           </tbody>
         </table>
       </div>
     @endforeach
shahidiqbal
  • 375
  • 1
  • 7