I have the following schema:
+------------+
| categories |
+------------+
| id |
+------------+
+-------------+
| sections |
+-------------+
| id |
| category_id |
+-------------+
+------------+
| questions |
+------------+
| id |
| section_id |
+------------+
+------------+
| clients |
+------------+
| id |
+------------+
+-------------------+
| client_questions |
+-------------------+
| client_id |
| question_id |
+-------------------+
As you can see questions are in sections and sections are in categories.
The admin of the system can toggle a question on or off for each individual client so I create client_questions to create the many-to-many relationship between clients and questions.
Now, I would like to harness the awesomeness of Eloquent to get a client's categories (to list out all the questions) but I can't seem to wrap my head around the query builder.
Basically I can currently do $client->questions via a many to many relationship I defined in the model:
public function questions() {
return $this->belongsToMany(Question::class,'client_questions','client_id','question_id');
}
but I can't seem to figure out how to do the same thing between clients and categories considering the degree of separation.
Basically I would like to do $client->categories
so that I can then list out the whole interview:
@foreach( $client->categories as $category)
@foreach( $category->sections as $section)
@foreach( $secion->questions as $question )
{{ $question->question }}
@endforeach
@endforeach
@endforeach