I'm working on a E-learning project and want to build a scalable relationships between tables but stuck on how to map them using eloquent relationship. i have 5 table
1. boards : id, name(field names)
2. standards: id, board_id, name
3. subjects: id, board_id, standard_id, name
4. chapters: id, board_id, standard_id, subject_id , name
5. questionTypes: id, type(like MCQ, T/F, Fill in the blanks)
6. questions: id,board_id, standard_id, subject_id, chapter_id, question_type_id, question
Description about structure
- boards represents the study board mean state board and all
- standards represents the class example: 1st 2nd etc
- subjects is like
math
,science
etc - chapters is like number system of math subject
- question_types represents type of question in this project i have 3 types of question but it can be more
- questions table contains all the questions of chapter depending upon
board
,standard
,subject
.
I'm using laravel 5 and i'm a newbee in eloquent relationships
You need to create models for each table:
php artisan make:model Board
Note: Laravel knows to pluralize your model so Model Board becomes Table boards. This also works for words like: Copy/copies, etc.
Artisan also creates a migration file for each model you create.
In this migration, you need to define foreign keys.
etc...
And in each Model file, you define the relationship:
And in the other models:
Now, in Laravel, you can do something like:
Board::find(1)->subjects
orSubject::find(4)->board
Hope this helps!