【问题标题】:HTML: Table does not create new row for my postsHTML:表格不会为我的帖子创建新行
【发布时间】:2018-09-29 01:07:33
【问题描述】:

我目前正在使用 Laravel 从数据库中检索帖子并将它们一一显示在表格中。每行将代表一个不同的帖子。它应该可以工作,但我不确定问题是什么,它只会创建 2 行。一行是列标题,所有帖子只有一行,而不是 7 行,例如 7 个帖子。

这是我在表格中显示帖子的代码:

<div class="container">
    <section class="row posts">
        <table class="post">
            <tr>
                <th>Title</th><th>Category</th><th>Description</th><th>Date Posted and Author</th>
            </tr>

            @foreach($posts as $post)
                <td>i</td> 
                <td>i</td> 
                <td>{{ $post->body }}</td>
                <td>Posted by {{ $post->user->first_name }} {{ $post->user->last_name }} on {{ $post->created_at }}</td>
            @endforeach
        </table>
    </section>
</div> 

忽略我放置“i”的前两列,它只是一个占位符。

【问题讨论】:

  • 很难在没有&lt;tr&gt; 表row 元素的情况下创建行:P
  • 您没有将 td 元素包装在 foreach 中的 tr 中

标签: html mysql laravel blade


【解决方案1】:

因为你没有使用&lt;tr&gt;标签

<div class="container">
    <section class="row posts">
        <table class="post">
            <tr>
                <th>Title</th><th>Category</th><th>Description</th><th>Date Posted and Author</th>
            </tr>

            @foreach($posts as $post)
                <tr>
                <td>i</td> 
                <td>i</td> 
                <td>{{ $post->body }}</td>
                <td>Posted by {{ $post->user->first_name }} {{ $post->user->last_name }} on {{ $post->created_at }}</td>
               </tr>
            @endforeach
        </table>
    </section>
</div> 

【讨论】:

    【解决方案2】:

    您忘记了循环中的&lt;tr&gt;。试试这个。。

     @foreach($posts as $post)
                <tr>
                <td>i</td> 
                <td>i</td> 
                <td>{{ $post->body }}</td>
                <td>Posted by {{ $post->user->first_name }} {{ $post->user->last_name }} on {{ $post->created_at }}</td>
                </tr>
            @endforeach
    

    【讨论】:

      【解决方案3】:
      <!--hope this works to create rows -->
      <div class="container">
      <section class="row posts">
          <table class="post">
              <tr>
                  <th>Title</th><th>Category</th><th>Description</th>
                   <th>Date Posted and Author</th>
              </tr>
      
              @foreach($posts as $post)
                    <tr>
                  <td>i</td> 
                  <td>i</td> 
                  <td>{{ $post->body }}</td>
                  <td>Posted by {{ $post->user->first_name }} 
                    {{ $post->user->last_name }} on 
                    {{ $post->created_at }}</td></tr>
              @endforeach
          </table>enter code here
         </section>
          </div> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多