anitakarst33
anitakarst33

Reputation: 33

Laravel show All Users in Table list / Display Correct

I want List all my Users in an Table, per line one Username.

But i get only the first User in this Table , all other Users are side by side.

My Blade:

<table class="table table-hover">

    <thead>

      <th>Username</th>

      <th>Orders</th>

      <th>Balance</th>

    </thead>

    <tbody>
@foreach($users as $user)



        <tr>

          <td>{{$user->username}} </td>

          <td>{{$user->purchases}} </td>

          <td>{{$user->balance}} </td>



        </tr>

    </tbody>

</table>

Controller:

 public function ShowUserlist(){


        $users = User::all();

        return view('profile.dashboard', compact('users'));
    }

The First User from my DB is in the <table class="table table-hover">

the other ones looks like: testtesttesttesttesttesttesttest under the Table.

How can i sort it per User one Line?

Thanks

Upvotes: 3

Views: 11929

Answers (4)

Lloople
Lloople

Reputation: 1844

You need to close the @foreach after each tr tag

<table class="table table-hover">

    <thead>

      <th>Username</th>

      <th>Orders</th>

      <th>Balance</th>

    </thead>

    <tbody>
@foreach($users as $user)

        <tr>

          <td>{{$user->username}} </td>

          <td>{{$user->purchases}} </td>

          <td>{{$user->balance}} </td>


        </tr>
@endforeach

    </tbody>

</table>

Upvotes: 2

Levente Otta
Levente Otta

Reputation: 795

You forgot to close foreach

<table class="table table-hover">
    <thead>
      <th>Username</th>
      <th>Orders</th>
      <th>Balance</th>
    </thead>
    <tbody>
        @foreach($users as $user)
        <tr>
          <td>{{$user->username}} </td>
          <td>{{$user->purchases}} </td>
          <td>{{$user->balance}} </td>
        </tr>
        @endforeach
    </tbody>
</table>

Upvotes: 0

Sohel0415
Sohel0415

Reputation: 9853

<table class="table table-hover">
 <thead>
  <tr>
   <th>Username</th>
   <th>Orders</th>
   <th>Balance</th>
  </tr>
 </thead>
 <tbody>

 @foreach($users as $user)
    <tr>
      <td>{{$user->username}} </td>
      <td>{{$user->purchases}} </td>
      <td>{{$user->balance}} </td>
    </tr>
 @endforeach
 </tbody>
</table>

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

Put @endforeach after </tr>:

        </tr>
    @endforeach
</tbody>

Upvotes: 0

Related Questions