2016-06-01 31 views
0

沒有獲得相關的模型預先加載的工作,但N + 1查詢的概率依然存在

一切似乎不錯,並與預先加載工作。如果沒有訪問與多態模型相關的模型。 Everything works well

但是,當我嘗試訪問其他模型

$俱樂部 - >事件() - > get()方法 - >計數()

<tbody> 
          @foreach($Clubs as $club) 
           <tr> 
            <td><a href="{{url('database/club/').'/'.$club->id}}">{{$club->club_name}}</a> 
            </td> 
            <td>{{$club->origin}}</td> 
            <td>{{$club->hp}}</td> 
            <td>{{$club->email}}</td> 
            <td>{{$club->location}}</td> 
            <td>{{$club->category}}</td> 
            <td>{{$club->rating}}</td> 
         //This cause N+1 problem to happen 
            <td>{{$club->events()->get()->count()}}</td> 

         //I will get more problem when access more!!!!! 
            {{--<td>{{$club->records()->get()->count()}}</td>--}} 

           </tr> 
          @endforeach 
</tbody> 

This is my database structure. The Records table will record all the participant from Event/Branch Event/Club

我的俱樂部控制器

public function index() 
{ 
    $Clubs = $this->club->with(['records','records.recordable', 'events'])->get(); 

    return view('pages.database.club', compact(['Clubs'])); 
} 

創型號

class Record extends Model 
{ 

    public function recordable() 
    { 
     return $this->morphTo(); 
    } 

    public function events() 
    { 
     return $this->morphedByMany('App\Event', 'recordable', 'records', 'recordable_id'); 
    } 

    public function sub_events() 
    { 
     return $this->morphedByMany('App\Sub_Event', 'recordable', 'records', 'recordable_id'); 
    } 

    public function clubs() 
    { 
     return $this->morphedByMany('App\Club', 'recordable', 'records', 'recordable_id'); 
    } 


    public function user() 
    { 
     return $this->belongsTo('App\User', 'user_id'); 
    } 

俱樂部模式

class Club extends Model 
{ 
    public function records() 
    { 
     return $this->morphMany('App\Record','recordable'); 
    } 

    // public function users() 
    // { 
    //  return $this->belongsToMany('App\User','club_user'); 
    // } 
    public function events() 
    { 
     return $this->hasMany('App\Event'); 
    } 

事件模型

class Event extends Model 
{ 
    public function sub_events() 
    { 
     return $this->hasMany('App\Sub_Event', 'event_id'); 
    } 

    public function records() 
    { 
     return $this->morphMany('App\Record', 'recordable'); 
    } 

    // public function users() 
    // { 
    //  return $this->belongsToMany('App\User', 'event_user'); 
    // } 

    public function club() 
    { 
     return $this->belongsTo('App\Club'); 
    } 

Refe rence

我試圖通過答案進行搜索,但它總是顯得過時。我嘗試了從 httpd stackoverflow.com/questions/26727088/laravel-eager-loading-polymorphic-relations-related-models 的解決方案,但仍無法解決此問題。

額外 我沒有足夠的聲譽後超過2張照片來形容我的問題

回答

1

你訪問你裝錯了的關係。執行時$club->events()->get()->count()您要求$club模型從數據庫中再次獲得關係而不是嘗試獲取已在加載中加載的關係。

正確的做法是將這兩行更改爲:$club->events->count()

+0

你是非常專業!你的答案很棒! 感謝您的幫助。你如何學習這些? –

+0

練習使完美:)你遇到的問題越多,隨着時間的推移,你獲得的知識越多。 –

+0

或者,您知道,請仔細閱讀[docs](http://laravel.com/docs)。 :d – Doom5