2017-10-08 44 views
1

我在控制器中有以下的相關性,它工作正常。laravel得到只從計數

$response=Event::with('eventDetail','eventDetail.members')->get(); 

現在我需要得到該事件的總成員。 事件有許多事件細節,每個事件細節都有許多成員。

Event::with(
    ['eventDetail'=>function($query){ 
     $query->count(); 
    }])->with([ 
    'eventDetail.members'=>function($query){ 
     $query->count(); 
    } 
    ] 
)->get(); 

但仍其裝載的所有數據,而不是數

回答

0

使用withCount()

Event::withCount(['eventDetail', 'eventDetail.members'])->get(); 
+0

,.thanks但IGOT eror調用未定義的方法應用程序\型號\事件:: withCount() – vision

+0

@vision默認工作,所以它看起來像你修改了一些東西。確保'Event'模型擴展'Model'類。 –