Reputation: 9
Hello i am a laravel beginner , when i do foreach its shows double of all single items what should i do with it code and ss are below
code
<div >
<h1 style="text-align: center">Lists of Outlets</h1>
@foreach($outlets as $outlet)
<table class="table table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>[email protected]</td>
</tr>
</tbody>
</table>
@endforeach
</div>
Upvotes: 1
Views: 65
Reputation: 26288
The issue is, you have put the entire table inside the loop, which is wrong. You have to only put the tr
inside the loop.
Try this:
@foreach($outlets as $outlet)
<tr>
<td>John</td>
<td>Doe</td>
<td>[email protected]</td>
</tr>
@endforeach
and one more thing, you are also using the static content inside the loop instead of using the loop variable value. So instead of:
<td>[email protected]</td>
it is something like:
<td>{{ $outlet['name'] }}</td> <!-- where name is the index in outlet array -->
Upvotes: 3
Reputation: 8249
@foreach
should be inside <tbody>
and instead of hard-coded content, you should be using $outlet
variable to get firstname, lastname and Email:
<div>
<h1 style="text-align: center">Lists of Outlets</h1>
<table class="table table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach($outlets as $outlet)
<tr>
<td>John</td>
<td>Doe</td>
<td>[email protected]</td>
</tr>
@endforeach
</tbody>
</table>
</div>
Upvotes: 1