【问题标题】:Matching leagues and matches from different tables(arrays)匹配来自不同表(数组)的联赛和比赛
【发布时间】:2017-05-30 13:10:03
【问题描述】:

我是 Laravel 的新手。

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

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

我需要像这样匹配tournamentsmatches(比赛、该比赛的比赛和该比赛的投注):

我通过将 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>

提前致谢!

【问题讨论】:

    标签: php mysql arrays laravel-5


    【解决方案1】:

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

    像在模型类中一样定义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
    

    【讨论】:

    • 嘿!感谢您的回答,但我将如何过滤结果?我需要做很多过滤和关系,这似乎很难做到。我也尝试了你的建议,但由于某种原因这个变成空了:$tournament->matches
    猜你喜欢
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多