【问题标题】:how to convert column data as table header laravel [closed]如何将列数据转换为表头 laravel [关闭]
【发布时间】:2020-12-19 10:35:00
【问题描述】:

嗨论坛如何将 created_at 列转换为标题表并将其与标题列同步。 这是我的数据库

下面是我的刀片代码

    <div class="container">           
    <table class="table table-striped"> 
        <thead>  
            <tr>
                    <th>No</th>
                    <th>Name</th>
                    @foreach($users as $u=>$user)
                        @foreach($user->posts as $p)
                        <th>{{$p->created_at}}</th>
                        @endforeach
                    @endforeach
            </tr>
        </thead>
          <tbody>
        @foreach($users as $u=>$user)
        <tr>
            <td>{{$u+1}}</td>
            <td>{{ $user->name }}</td>
            @foreach($user->posts as $p)
            <td><a href ="/posts/{{$p->id}}">{{$p->title}}</a></td>
        </tr>
        @endforeach
          </tbody>
    </table>
  </div> 

这是我的刀片视图

我有 2 个表,用户表和发布表。一个用户可以有很多帖子。

这是我的帖子模型

这是我的用户模型

这是我的帖子控制器

谢谢。

【问题讨论】:

  • 你的问题不太清楚。请将您的代码添加为代码而不是图像。并使用您预期的输出格式更具体地解释您的问题。
  • 代码不要放图片,代码是文字,可以复制粘贴到代码块中

标签: php html mysql laravel eloquent


【解决方案1】:

您可以在 @php 指令的帮助下将所有 created_at 收集到一个新数组中,并在显示时使用 2 个嵌套循环进行比较。

遍历所有created_at 并检查是否有任何帖子的时间匹配。如果是,则显示锚标记并跳出循环。如果它们都不匹配,则打印一个空的td

<div class="container">           
<table class="table table-striped"> 
    <thead>  
        <tr>
                <th>No</th>
                <th>Name</th>
                @php
                    $created_ats = []
                @endphp
                @foreach($users as $u => $user)
                    @foreach($user->posts as $p)
                    <th>{{ $p->created_at }}</th>
                    @php
                        $created_ats[] = $p->created_at
                    @endphp
                    @endforeach
                @endforeach
        </tr>
    </thead>
    <tbody>
    @foreach($users as $u => $user)
        <tr>
            <td>{{$u+1}}</td>
            <td>{{ $user->name }}</td>
            @foreach($created_ats as $created_at)
                @foreach($user->posts as $p)
                    @if($p->created_at == $created_at)
                        <td><a href ="/posts/{{$p->id}}">{{$p->title}}</a></td>
                        @break
                    @endif
                    @if($loop->last)
                        <td></td>
                    @endif
                @endforeach         
            @endforeach        
        </tr> 
    @endforeach   
    </tbody>
</table>
</div> 

如果您想要排序格式的日期,请在您的控制器中收集所有日期,usortDateTime 类的帮助下将它们传递给视图。

控制器代码:

<?php 

public function index(){
    $users = User:all();
    $created_ats = [];
    foreach($users as $u=>$user){
        foreach($user->posts as $p){
            $created_ats[] = $p->created_at;
        }
    }

    usort($created_ats,function($date1,$date2){
        $date1 = \DateTime::createFromFormat('Y-m-d H:i:s', $date1);
        $date2 = \DateTime::createFromFormat('Y-m-d H:i:s', $date2);
        return $date1 <=> $date2; // needs PHP7 minimum
    });

    return view('posts.index',compact('users','created_ats'));
}

刀片:

<div class="container">           
<table class="table table-striped"> 
    <thead>  
        <tr>
                <th>No</th>
                <th>Name</th>
                @foreach($created_ats as $created_at)
                    <th>{{ $created_at }}</th>
                @endforeach
        </tr>
    </thead>
    <tbody>
    @foreach($users as $u => $user)
        <tr>
            <td>{{$u+1}}</td>
            <td>{{ $user->name }}</td>
            @foreach($created_ats as $created_at)
                @foreach($user->posts as $p)
                    @if($p->created_at == $created_at)
                        <td><a href ="/posts/{{$p->id}}">{{$p->title}}</a></td>
                        @break
                    @endif
                    @if($loop->last)
                        <td></td>
                    @endif
                @endforeach         
            @endforeach        
        </tr> 
    @endforeach   
    </tbody>
</table>
</div>

【讨论】:

  • 哇哦谢谢@nice_dev。我试图解决这个问题已经有一年多了。顺便说一句,作为最后的乞求,你想建议我将标题从较低的日期到较高的日期 1、2、3、45、5、....等等
  • @Enthusiast 你必须sort它们,将它们收集在一个数组中,然后将它们传递给视图。
  • brohhh 我知道这个概念,但不知道代码技术,据我所知,它可能正在使用带有 groupBy 或 orderBy 的查询生成器。如果你不介意让我知道methode() 或propherties()。提前谢谢你。
  • 谢谢兄弟@nice_dev。一切都按预期工作。来自印度尼西亚的问候
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 2019-12-06
  • 2023-04-02
  • 2019-09-07
  • 1970-01-01
  • 2017-09-12
  • 2014-01-26
相关资源
最近更新 更多