如何将列数据转换为表标题

时间:2020-08-31 09:52:36

标签: php html mysql laravel eloquent

hi论坛中如何将created_at列转换为标题表并将其与title列同步。 这是我的数据库 my data base

下面是我的刀片服务器代码

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

这是我的刀片视图 my blade view

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

这是我的帖子模型

my post model

这是我的用户模型

user model

这是我的职位控制者

my post controller

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以借助@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> 

如果要以排序的格式显示日期,请在控制器中收集所有日期,并在usort类的帮助下DateTime将日期传递给视图。

控制器代码:

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