How to split a loop into multiple column and row?

948 views Asked by At

I have a loop to display my data in table format.

<table>
<thead>
<th>name</th>
<th>qty</th>
</thead>
<tbody>
  @foreach ($users as $user)
      @foreach ($user->displayDetails($user->id) as $d)
       <tr>
          <td>{{$d->name}}</td>
          <td>{{$d->ordered_qty}}</td>                             
       </tr>
     @endforeach
    @endforeach
</tbody>
</table>

The result output will be

name | qty
John   1
Lynn   1
Jack   2

What I am expecting is:

name | qty    name | qty
John   1      Lynn   1
Jack   2

How can I make this?

1

There are 1 answers

2
Taha Paksu On BEST ANSWER

You can use something like this: (It's much HTML splitting instead of Laravel)

<table>
    <thead>
        <th>name</th>
        <th>qty</th>
        <th>name</th>
        <th>qty</th>
    </thead>
    <tr>
    @foreach ($users as $key => $user)
        @foreach ($user->displayDetails($user->id) as $d)
        <td>{{$d->name}}</td>
        <td>{{$d->ordered_qty}}</td>                             
        @endforeach
    @if((@key + 1) % 2 == 0)
    </tr><tr>
    @endif
    @endforeach
    </tr>
</table>