2017-09-15 117 views
1

你好,我是一個laravel初學者,當我這樣做的foreach其顯示一倍所有單品我應該怎麼用它做代碼和SS都低於laravel 5.4表指導需要

Brower image

代碼

<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> 

回答

3

問題是,你把整個表放在循環內,這是錯誤的。你只需要將tr放入循環中。

試試這個:

@foreach($outlets as $outlet) 
<tr> 
    <td>John</td> 
    <td>Doe</td> 
    <td>[email protected]</td> 
</tr> 
@endforeach 

還有一件事,你還使用循環內的靜態內容,而不是使用循環變量值。因此,而不是:

<td>[email protected]</td> 

它是這樣的:

<td>{{ $outlet['name'] }}</td> <!-- where name is the index in outlet array --> 
+0

看起來另外一個問題是,它實際上並沒有使用'$ outlet'中的任何內容,而是每一個都使用相同的靜態值。 –

+0

@ Don'tPanic你是對的 –

1

@foreach應該是內部<tbody>和,而不是硬編碼的內容,你應該使用$outlet變量獲得名字,姓氏和電子郵件:

<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>