Hello,
Among the tables, three are interesting to understand my problem.
1rst table : states (id, name, color)
2nd table : sessions (id, date, state_id)
3rd table : students (id, name)
The number of states is dynamic (in the database), so I can add some new states if needed.
When I display the view of a student, I want to display some statistics with the number of sessions of each state.
$student = Student::
with('sessions')
->withCount([
'sessions as sessions_state_1_count' => function ($query) {
$query->where('state_id', ...);
}])
->find($id);
But the content of the withCount has to be dynamic according to the number of states in the database.
Further more the withCount has to be about the sessions and not the students. So my query is false, my withCount is not at the right place. I'm always clumsy with such queries with Eloquent.
Do you have any idea how to do that ?
Thanks a lot ;).
Vincent