【发布时间】:2017-05-30 13:10:03
【问题描述】:
我是 Laravel 的新手。
我有一个包含这些表的数据库:
- tournaments(leagues)
- matches(with tournament_id)
- bets(with match_id)
我需要像这样匹配tournaments 和matches(比赛、该比赛的比赛和该比赛的投注):
我通过将 tournaments 和 matches 放入两个不同的数组并使用 foreach 嵌套打印出它们来做到这一点。
但是问题是当我们匹配太多时,页面加载时间会上升到 15 秒。
这是我的控制器(我知道这不是你见过的最好的代码)
$tournaments = \DB::table('matches as m')
->select([
'm.tournament_id as tournament_id',
'm.status_desc as status_desc',
't.results_name as tournament_name',
'c.name as country',
'c.code as country_code',
't.is_popular as popular',
\DB::raw('count(b.id) as count'),
])
->whereRaw('DATE(m.date_hour) = DATE(NOW())')
->where('m.status_type', '=','notstarted')
->join('tournaments as t','m.tournament_id','=','t.id')
// ->leftJoin('bets as b','m.id','=','b.match_id')
->groupBy('t.id')
->join('countries as c','t.country_id','=','c.id')
->leftJoin('bets as b','m.id','=','b.match_id')
->orderBy('t.is_popular', 'desc')
->orderBy(\DB::raw('count(b.id)'), 'desc')
->orderBy('c.name', 'asc')
->get();
$matches = \DB::table('matches as m')
->select([ 'm.id as match_id',
'm.date_hour as date',
'm.tournament_id as tournament_id',
'm.host_id as host_id',
'm.guest_id as guest_id',
'm.status_desc as status_desc',
't.results_name as tournament_name',
'c.name as country',
'host.name as host_name',
'guest.name as guest_name',
\DB::raw('count(b.id) as count'),
])
->whereRaw('DATE(m.date_hour) = DATE(NOW())')
->where('m.status_type', '=','notstarted')
->join('tournaments as t','m.tournament_id','=','t.id')
->join('countries as c','t.country_id','=','c.id')
->join('teams as host','host.id','=','m.host_id')
->join('teams as guest','guest.id','=','m.guest_id')
->leftJoin('bets as b','m.id','=','b.match_id')
->groupBy('m.id')
->get();
这是我的观点:
@foreach($tournaments as $tournament)
<thead>
<tr data-toggle="collapse" data-target="#accordion-{{$tournament->tournament_id}}" class="clickable">
<th style="background-color: #b8312f; color:#fff;text-align:center; " colspan="4">
<div class="pull-left"><span class="flag-icon flag-icon-{{strtolower($tournament->country_code)}}"></span></div>{{ $tournament->country.' - '.$tournament->tournament_name}}
<a class="btn btn-default btn-xs pull-right">
<i class="fa fa-angle-double-down" title="Align Justify"></i>
</a></th>
</tr>
</thead>
@if($tournament->popular == 1 || $tournament->count > 0 )
<tbody id="accordion-{{$tournament->tournament_id}}" class="collapse in" area-expanded="true" >
@else
<tbody id="accordion-{{$tournament->tournament_id}}" class="collapse">
@endif
@foreach($matches as $match)
@if($match->tournament_id == $tournament->tournament_id && $match->count > 0 )
<tr class="clickable_row" data-url="{{ URL::route('matches.view',[$match->match_id,str_slug($match->host_name.' vs '.$match->guest_name)])}}" style="cursor: pointer">
<td><div class="pull-right"> {{$match->host_name}}</div></td>
<td style="width:50px!important; background-color:#CCD1D1">{!!(new \Carbon\Carbon($match->date))->format('H:i')!!}</td>
<td><div class="pull-left"> {{$match->guest_name}} </div></td>
<td style="width:20px!important"><span class="label @if($match->count > 0) label-success @else label-warning @endif">{{$match->count}} @if($match->count == 0 || $match->count == 1) Prediction @else Predictions @endif </span>
</td>
</div> </tr>
@endif
@endforeach
</tbody>
@endforeach
</table>
提前致谢!
【问题讨论】:
标签: php mysql arrays laravel-5