我有桌子:
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
我想要的是显示按分支和区域分组的用户发送的所有待处理。所以我的查询就像选择所有区域一样,在循环内选择与region_id
,inner join
users
tbl匹配的分支以获取user_id
并在其中选择所有与匹配user_id
匹配的待处理状态,如果查询返回docdate
,则显示NOT NULL
,否则显示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
嗯,这正是我想要的。但是,那个逻辑在我的控制器里面。我希望它在我看来。我怎样才能在我看来回归那个逻辑?有什么办法吗?在我看来,我所能做的就是@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>
答案 0 :(得分:1)
让我们把它分解。
您可以在Region模型上映射这种关系,并将其称为'pendings',因此您可以级联这些实现以使其更具可读性,只需调用Region :: with('branches') - &gt; get();并让它返回你想要的东西。看看吧。
我们将如何做到这一点。
//Region.php
function branches(){
return $this->hasMany('App\Branch')->with('usermachines');
}
这是棘手的部分,让我们假装用户表是分支和挂件之间的多对多关系表,并使用分支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但是当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 ($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>