Foreach loop (laravel blade)

I need help with a foreach loop in laravel. For some reason I need to have two rows instead of two tables (would have solved my problem) However, I have problems with it getting to work due to not knowing where to place my tags to get the right table structure.

Code:

<table class="minimalistBlack">
    <tbody>
        <tr>
            <th>Etternavn</th>
            <th>Navn</th>
            <th>Posthylle</th>
            <th>X</th>
            <th>Etternavn</th>
            <th>Navn</th>
            <th>Posthylle</th>

        </tr>
    @foreach ($data as $mailbox)
        <td>{{ $mailbox->last_name }}</td>
        <td>{{ $mailbox->first_name }}</td>
        <td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
        <td></td>

    @endforeach
    @foreach ($data2 as $mailbox)
        <td>{{ $mailbox->last_name }}</td>
        <td>{{ $mailbox->first_name }}</td>
        <td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
    @endforeach

    </tbody>
</table>

How can I combine the two foreach loops and and get the right table structure?


to achieve just that result you could do

foreach (array_combine($courses, $sections) as $course => $section)

but that only works for two arrays

or u can use two table and make feel as single table

<div class="row">
<div class="col-md-6 col-sm-6">
<table class="table table-striped">
 @foreach ($data as $mailbox)
  <td>{{ $mailbox->last_name }}</td>
  <td>{{ $mailbox->first_name }}</td>
  <td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
  <td></td>

 @endforeach
</table>
</div>
<div class="col-md-6 col-sm-6">
<table class="table table-striped">
 @foreach ($data as $mailbox)
  <td>{{ $mailbox->last_name }}</td>
  <td>{{ $mailbox->first_name }}</td>
  <td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
  <td></td>

 @endforeach
</table>
</div>


Assuming the $data and $data2 variables are Laravel collection instances, you can simply use $data = $data->concat($data2) . Now $data will contain data of both variables. For a simple array, you can use $data += $data2; . This will merge $data & $data2 to $data . Then you can simply use:

...
@foreach ($data as $mailbox)
<tr>
  <td>{{ $mailbox->last_name }}</td>
  <td>{{ $mailbox->first_name }}</td>
  <td>{{ $mailbox->row }}{{ $mailbox->number }}</td>
</tr>
@endforeach
</tbody>
</table>

像这样的控制器中的用户array_merge功能

$array1 = array("john","ny");
$array2 = array("mitchel","ny");
$result = array_merge($a1,$a2);
链接地址: http://www.djcxy.com/p/53076.html

上一篇: PHP Laravel 2次破解foreach循环

下一篇: Foreach循环(laravel blade)