該模型結構如下Laravel 5.3 withCount()嵌套關係
教程 - >(的hasMany)章節 - >(的hasMany)視頻
我們怎樣才能加載的從教程模型視頻(VIDEO_COUNT)數與laravel 5.3的withCount()方法
我曾嘗試:
Tutorial::withCount('chapters')
->withCount('chapters.videos') // this gives error: Call to undefined method Illuminate\Database\Query\Builder::chapters.videos()
->all();
編輯
這有效,任何更好的解決方案?
Tutorial::withCount('chapters')
->with(['chapters' => function($query){
$query->withCount('videos');
}])
->all();
有你定義模型的關係? –
你只需要做一個 - > withCount('chapters.videos')。另外,確保您的關係設置正確。 – 2016-09-22 15:22:24
@DigitalFire關係是正確的,因爲我可以加載他們急切的加載。只是計數不會與withCount()方法填充 – crazy1337