Laravel-根据何处原因进行表联接和计数

时间:2019-03-01 03:59:57

标签: laravel model

我正在一个项目上,我的代码开始变得混乱。

当前我的控制器上有以下内容

$rollmonth = Rollmapping::latest()->value('roll_month');

    $officerwk1 = DB::table('roll')
            ->join('rollmapping', "roll.roll_id", "=", "rollmapping.id")
            ->join('members', 'roll.member_id', '=', 'members.id')
            ->where('rollmapping.roll_month','=', $rollmonth)
            ->where('roll.status', '!=', 'A')
            ->where('members.rank', '<', 12)
            ->where('rollmapping.roll_week', '=', 1)
            ->get();

问题是我要重复5次(每月的每个星期) 唯一的改变是

->where('rollmapping.roll_week', '=', 1)

每周1、2、3、4、5,

我还将为每个等级组(它们是4)重复一次

例如 WO / TO将具有以下内容:

->wherebetween('members.rank', [12,13])

NCO将具有以下内容:

->wherebetween('members.rank', [14,18])

学员将具有以下内容:

->where('members.rank', '>', 18)

这将导致25个不同的变量,这不合适 使用模型必须有更好的方法

转台

|id|roll_id|member_id|status|

映射表

|id|roll_date|roll_year|roll_month|roll_week|

会员表

|id|first_name|last_name|rank|.....

当然,必须有一个更好的解决方案来提供25个不同的变量,这些变量将显示为类似这样的计数

|         |Week 1|Week 2|Week 3|week 4|week 5|Totals|
|Offciers |Count  Count  Count  Count  Count |Total |
|TO/WO    |Count  Count  Count  Count  Count |Total | 
|NCO's    |Count  Count  Count  Count  Count |Total |
|Cadets   |Count  Count  Count  Count  Count |Total |
|Total    |Total  Total  Total  Total  Total |Total |

此刻我的桌子看起来像这样;

<table class="table">
                        <tr>
                            <th class="text-center">Details</th>
                            <th class="text-center">Week 1</th>
                            <th class="text-center">Week 2</th>
                            <th class="text-center">Week 3</th>
                            <th class="text-center">Week 4</th>
                            <th class="text-center">Week 5</th>
                            <th class="text-center">Total</th>
                        </tr>
                        <tr>
                            <th class="text-center">Officer:</th>
                            <td class="text-center">{{$officerwk1->count()}}</td>
                            <td class="text-center">{{$officerwk2->count()}}</td>
                            <td class="text-center">{{$officerwk3->count()}}</td>
                            <td class="text-center">{{$officerwk4->count()}}</td>
                            <td class="text-center">{{$officerwk5->count()}}</td>
                            <td class="text-center">{{$totalofficer->count()}}</td>
                        </tr>
                        <tr>
                            <th class="text-center">TO/WO:</th>
                            <td class="text-center">{{$towk1->count()}}</td>
                            <td class="text-center">{{$towk2->count()}}</td>
                            <td class="text-center">{{$towk3->count()}}</td>
                            <td class="text-center">{{$towk4->count()}}</td>
                            <td class="text-center">{{$towk5->count()}}</td>
                            <td class="text-center">{{$totalto->count()}}</td>
                        </tr>
                        <tr>
                            <th class="text-center">NCO:</th>
                            <td class="text-center">{{$ncowk1->count()}}</td>
                            <td class="text-center">{{$ncowk2->count()}}</td>
                            <td class="text-center">{{$ncowk3->count()}}</td>
                            <td class="text-center">{{$ncowk4->count()}}</td>
                            <td class="text-center">{{$ncowk5->count()}}</td>
                            <td class="text-center">{{$totalnco->count()}}</td>
                        </tr>
                        <tr>
                            <th class="text-center">Cadets:</th>
                            <td class="text-center">{{$cadetwk1->count()}}</td>
                            <td class="text-center">{{$cadetwk2->count()}}</td>
                            <td class="text-center">{{$cadetwk3->count()}}</td>
                            <td class="text-center">{{$cadetwk4->count()}}</td>
                            <td class="text-center">{{$cadetwk5->count()}}</td>
                            <td class="text-center">{{$totalcadet->count()}}</td>
                        </tr>
                        <tr>
                            <th class="text-center">Total:</th>
                            <td class="text-center">{{$totalwk1->count()}}</td>
                            <td class="text-center">{{$totalwk2->count()}}</td>
                            <td class="text-center">{{$totalwk3->count()}}</td>
                            <td class="text-center">{{$totalwk4->count()}}</td>
                            <td class="text-center">{{$totalwk5->count()}}</td>
                            <td class="text-center">{{$total->count()}}</td>
                        </tr>
                    </table>

任何帮助都会很棒

0 个答案:

没有答案