Hassam Ali
Hassam Ali

Reputation: 9

laravel 5.4 table guidance needed

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

Brower image

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

Answers (2)

Mayank Pandeyz
Mayank Pandeyz

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

Milan Chheda
Milan Chheda

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

Related Questions