具有动态列的Laravel刀片显示器

时间:2019-05-01 18:33:36

标签: laravel laravel-blade

我使用DB :: select在控制器中成功进行了数据库查询,这为我提供了一系列经过验证的不错的结果。我想在视图中进行排列,这样我就可以查看按数据排序的月份,以及按特定组合查看数据库中的一组动态列的时间。我能够在刀片中对值进行硬编码,但是显然这对动态列不起作用。

我提供了代码,并且也做了一些额外的mysql查询,但无济于事。我在论坛上读了无数的帖子,没有发现任何适用的内容,所以我认为我会发布。

$monthly_paid = DB::select('select year(date) as year, month(date) as month, sum(payment_amount) as total_monthly, p.portfolio_name as portfolio, 
payment_amount, paid from funds
join portfolios as p where funds.portfolio_id = p.id and hide = 0
group by year(date), month(date), portfolio');
dd($monthly_paid);

查询结果:

array:12 [▼
0 => {#217 ▼
+"year": 2018
+"month": 1
+"total_monthly": 2900.0
+"portfolio": "TW-CS"
+"payment_amount": "2900"
+"paid": 1
} ... etc.

这是我的刀片尝试:

 @foreach ($monthly_paid as $monthly)
        <div class="row">
                <?php $monthNum = $monthly->month;
                        $dateObj = DateTime::createFromFormat('!m', $monthNum);
                        $monthName = $dateObj->format('F');?>
                <div class="col-md-1">{{ $monthly->year }}</div>
                <div class="col-md-1">{{ $monthName }}</div>
                <div class="col-md-1">
                @if($monthly->portfolio == "TW-CS")
                @if($monthly->paid == 1)
                <button type="button" class="btn btn-success btn-sm">
                        ${{ number_format($monthly->payment_amount) }}
                </button>
                @else
                ${{ number_format($monthly->payment_amount) }}
                @endif
                @endif
                </div>
                <div class="col-md-1">
                @if($monthly->portfolio == "TW-C12")
                @if($monthly->paid == 1)
                <button type="button" class="btn btn-success btn-sm">
                        ${{ number_format($monthly->payment_amount) }}
                </button>
                @else
                ${{ number_format($monthly->payment_amount) }}
                @endif
                @endif
 </div>
                <div class="col-md-1">
                @if($monthly->portfolio == "TW-C22")
                @if($monthly->paid == 1)
                <button type="button" class="btn btn-success btn-sm">
                        ${{ number_format($monthly->payment_amount) }}
                </button>
                @else
                ${{ number_format($monthly->payment_amount) }}
                @endif
                @endif
                </div>
                <div class="col-md-2" style="background-color:#333333; color:#FFFFFF;">${{ number_format($monthly->total_monthly) }}</div>
        </div>
        @endforeach

我正在尝试获取以下格式:

    TW-CS     TW-C12    TW-C22    TOTAL AMOUNT PAID
    $7500     $7800     $8200     $23,500

第一列是月份,第二列是投资组合1,第三列是投资组合2,第四列是投资组合3,最后一列是总和。

显然,它经过硬编码,可以正确显示,但是我正在尝试使其自动化。

1 个答案:

答案 0 :(得分:0)

在此示例中,您可以从动态模型获得动态列。以及它的模型关系

对于型号

public function  scopeCustomSelect( $query ,$items=[])
{
    $stu_class_col=['id as stu_class_id','std_id']; // Required else retional model will not retun data. here id and std_id is primary key and foreign key.
    $stu_doc_col=['id as stu_doc_id','std_id'];// Required else retional model will not retun data. here id and std_id is primary key and foreign key.

    foreach ( $items as $col)
    {
        if(  Schema::hasColumn('student_information', $col ))
        {
          $stu_info_col[]= $col ;
        }
        elseif (  Schema::hasColumn('student_class',$col))
        {
            $stu_class_col[]= $col ;
        }
        elseif (  Schema::hasColumn('student_image',$col))
        {
            $stu_doc_col[]= $col ;
        }
    }
// converting array to string for bind column into with relation...
    $stu_class_col_string =  implode(',',$stu_class_col);
    $stu_doc_col_string =  implode(',',$stu_doc_col); 

    return $colQuery = $query->select($stu_info_col)
                    ->with(["student_class:$stu_class_col_string", "studentImage:$stu_doc_col_string"]);
}

对于控制器

 $studentInfo =  Student::whereHas("student_class",function($q) use($req){
                                   $q->where("std_session",$req->session_code);
                                   $q ->where("std_class",$req->class_code);
                                   $q ->where("std_section",$req->std_section); })
                           ->customSelect($fields['dataList'])
                           ->get();