匹配来自不同表(阵列)的联赛和比赛

时间:2017-01-15 17:02:09

标签: php mysql arrays laravel-5

我很喜欢 Laravel

我有一个包含这些表的数据库:

 - tournaments(leagues)  
 - matches(with tournament_id)
 - bets(with match_id) 

我需要匹配tournamentsmatches这样的比赛(锦标赛,该锦标赛的比赛以及为该比赛下注的赌注):

enter image description here

我是通过将tournamentsmatches放入两个不同的数组并使用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>

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以通过在模型中使用关系来实现此目的。

在模型类中定义hasMany()关系。

试试吧..

Tournament.php

class Tournament extends Model
{
    .........
    .........

    public function matches(){
        return $this->hasMany('App\Match', 'tournament_id', 'tournament_id');
    }
}

Match.php

class Match extends Model
{
    .........
    .........
    public function bets(){
        return $this->hasMany('App\Bet', 'match_id', 'match_id');
    }
}

Bet.php

class Bet extends Model
{
    .........
    .........
}

从控制器传递数据,例如

$tournaments = Tournament::all();
在您的视图中

只执行foreach之类的

@foreach($tournaments as $tournament)
    //diaplay your tournament UI

    @foreach($tournament->matches as $match)
        //diaplay your match UI
        //display count by `count($match->bets)`
    @enforeach

@endforeach