Return 从控制器到视图的逻辑 Laravel 5

Return logic from Controller into View Laravel 5

我有桌子:

REGIONS

 id | name
----+------------------
 1  | South Luzon
----+------------------
 2  | North West Luzon
----+------------------
 3  | North East Luzon

=====================================

BRANCHES

machinenum | name      | region_id 
-----------+-----------+-----------
108        | Alaminos  | 1
-----------+-----------+-----------
104        | Alexander | 3
-----------+-----------+-----------
131        | Santiago  | 3
-----------+-----------+-----------
114        | Apalit    | 1
-----------+-----------+-----------
137        | Baliuag   | 1
-----------+-----------+-----------
115        | Baguio    | 2
-----------+-----------+-----------
116        | Bantay    | 2
-----------+-----------+-----------
130        | San Jose  | 3

=======================================

USERS

id | name  | machinenum
---+-------+-------------
1  | user1 | 108
---+-------+-------------
2  | user2 | 104
---+-------+-------------
3  | user3 | 131

========================================

PENDINGS

user_id | docdate 
--------+------------
2       | 2016-07-14
--------+------------
1       | 2016-07-13
--------+------------
1       | 2016-07-14
--------+------------
3       | 2016-07-13

我想要的是显示所有按分支和地区分组的用户发送的待处理信息。所以我的查询就像 selecting 所有区域,在一个循环 select 分支内匹配 region_idinner join users tbl 得到 user_id 并且在其中,select 所有与 user_id 匹配的未决项,如果查询 returns NOT NULL 则显示 docdate 否则显示 0.

这是我在控制器中的查询:

$regions = DB::select('SELECT * FROM regions');
        foreach ($regions as $region) {
            echo $region->name . "<br>";
            $branches = DB::select('SELECT b.machinenum, b.name AS bname, u.id as uid
                                    FROM branches AS b
                                    INNER JOIN users AS u ON b.machinenum=u.machinenum
                                    WHERE region_id=:id
                                    ORDER BY b.name ASC',
                                    ['id' => $region->id]);
            foreach ($branches as $branch) {
                echo $branch->bname . "<br>";
                $pendings = DB::select('SELECT * FROM pendings WHERE user_id=:id', ['id' => $branch->uid]);
                if ($pendings) {
                    foreach ($pendings as $pending) {
                        echo $pending->docdate . "<br>";
                    }
                    echo "<br>";
                } else {
                    echo "0 <br>";
                }
            }
            echo "<br>";
        }

结果将是:

South Luzon
Alaminos
2016-07-14 -- docdate

Apalit
0          -- return 0 if no pending
Baliuag
0          -- return 0 if no pending

North West Luzon
Baguio
0          -- return 0 if no pending
Bantay
0          -- return 0 if no pending

North East Luzon
Alexander
2016-07-13 -- docdate
2016-07-14 -- docdate

San Jose
0          -- return 0 if no pending
Santiago
2016-07-13 -- docdate

嗯,这正是我想要的。但是,该逻辑在我的控制器中。我希望它在我看来。在我看来,我怎么能 return 这种逻辑呢?有什么办法吗?在我看来我能做的就是@foreach and @if.

这是我目前的代码(不要介意用户和分支之间的关系,我已经在我的模型中):

// Note! In controller I have:

$regions = Region::all();
$branches = Branch::all();
$pendings = Pending::all();
return view('pending.index', compact('regions', 'branches', 'pendings'));



<table class="table table-noborder table-extra-condensed">
    <thead>
        <tr>
            <th class="custom-td text-center">Date</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($regions as $region)
            <tr>
                <th colspan="19">{{ $region->name }}</th>
            </tr>
            @foreach ($branches as $branch)
                @if ($region->id === $branch->region_id)
                    <tr>
                        <td>{{ $branch->name }}</td>
                    </tr>
                    @foreach ($pendings as $pending)
                        @if ($branch->machinenum === $pending->user->machinenum)
                            <tr>
                                <td class="custom-td text-center">{{ $pending->docdate->format('d') }}</td>
                            </tr>
                        @endif
                    @endforeach
                @endif
            @endforeach
        @endforeach
    </tbody>
</table>

让我们分解一下。

您可以将这种关系映射到您的区域模型上,并将其命名为 'pendings',这样您就可以级联这些关系以使其更具可读性,并且只需调用 Region::with('branches')- >得到();并拥有 return 想要的东西。看看吧。

以下是我们的操作方法。

//Region.php
function branches(){
    return $this->hasMany('App\Branch')->with('usermachines');
}

这是棘手的部分,让我们假设用户 table 是分支和挂起之间的多对多关系 table 并使用分支 hasManyThrough 为我们完成困难的部分:

//Branch.php
function usermachines(){
    return $this->hasManyThrough('App\Pending','App\User','machinenum','user_id')->with('pendings');
}

完成!通过所有这些设置,您需要做的就是:

在你的控制器中:

$regions = Region::with('branches')->get();
return view('pending.index', compact('regions'));

在您看来: 我正在删除你的第二个 foreach 并使用一个 forelse,它就像 foreach 但在待处理为零时有一个 else,检查一下:

<table class="table table-noborder table-extra-condensed">
<thead>
    <tr>
        <th class="custom-td text-center">Date</th>
    </tr>
</thead>
<tbody>
    @foreach ($regions as $region)
        <tr>
            <th colspan="19">{{ $region->name }}</th>
        </tr>
        @foreach ($region->branches as $branch)
                <tr>
                    <td>{{ $branch->name }}</td>
                </tr>
                @forelse($branch->pendings as $pending)
                        <tr>
                            <td class="custom-td text-center">{{ $pending->docdate->format('d') }}</td>
                        </tr>
                @empty
                        <tr>
                           <td>0</td>
                        </tr>
                @endforelse
        @endforeach
    @endforeach
</tbody>