简体   繁体   中英

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>

User array_merge function in controller like this

$array1 = array("john","ny");
$array2 = array("mitchel","ny");
$result = array_merge($a1,$a2);

if $data and $data2 have one, one row you can try like this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM