例
受診データ (hasOne) -> 患者 (hasMany)-> 担当医(複数)
こちらの例のように、リレーション先の患者にさらにリレーションで担当医が複数いる場合の担当医の数を取得します。
$data_loop = $model->with('patient')->get();
↓ このように追加します
$data_loop = $model->with(['patient' => function($query){
$query->with('doctors');
}])->get();
$data_loop = $model->with(['patient' => function($query){
$query->withCount('doctors');
}])->get();
● Laravel で リレーション先のさらに先のリレーションの件数を検索条件にする。
「doctors を2人以上持つ」「patient」のデータを取得する
$data_loop = $model->with(['patient' => function($query){
$query->with('doctors');
}])->has('patient.doctors','>',1)get();