如何在视图上生成动态表列

时间:2019-01-29 05:54:45

标签: mysql laravel-5

我需要从数据库表生成这种表。 enter image description here

这是我查询的输出集合

[{delivery_date: "2019-01-09", delivery_count: "5", price: "190.00", total_price: "950.00"}
{delivery_date: "2019-01-10", delivery_count: "45", price: "170.00", total_price: "7650.00"}
{delivery_date: "2019-01-10", delivery_count: "2", price: "50.00", total_price: "100.00"}
{delivery_date: "2019-01-10", delivery_count: "3", price: "60.00", total_price: "180.00"}
{delivery_date: "2019-01-10", delivery_count: "3", price: "185.00", total_price: "555.00"}
{delivery_date: "2019-01-16", delivery_count: "6", price: "50.00", total_price: "300.00"}]

这就是我查询表的方式

 $deliveries = DB::table('deliveries')
    ->where('user_id',Auth::id())
    ->groupBy('price','delivery_date')
    ->orderBy('delivery_date')
    ->select('delivery_date',DB::raw('SUM(number) as delivery_count'),'price', DB::raw('SUM(number*price) as total_price'))
    ->get();

这就是我显示数据的方式

<table class="display table table-condensed table-bordered">
    <thead>
         <th>Date</th>
         @foreach($deliveries as $delivery)
             <th>{{$delivery->price}}</th>
         @endforeach
    </thead>
    <tbody>

         @foreach($deliveries as $delivery) 
              <tr>      
                  <td>{{ $delivery->delivery_date }}</td>   
                   @if($delivery->price = $deliveries[$i]->price)
                        <td>{{ $delivery->delivery_count }}</td>   
                   @else
                        <td>0</td>   
                   @endif                        
              </tr>
         @endforeach

     </tbody>
 </table>

这就是我的看法

enter image description here

我被困在这里,仍在尝试找出方法。.请帮助。  谢谢大家!

0 个答案:

没有答案