php - laravel 5 : scalable relationship between more than 4 tables -


i'm working on e-learning project , want build scalable relationships between tables stuck on how map them using eloquent relationship. 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 blanks)  6. questions: id,board_id, standard_id, subject_id, chapter_id, question_type_id, question  

description structure

  • boards represents study board mean state board , all
  • standards represents class example: 1st 2nd etc
  • subjects math , science etc
  • chapters number system of math subject
  • question_types represents type of question in project have 3 types of question can more
  • questions table contains questions of chapter depending upon board, standard, subject .

i'm using laravel 5 , i'm newbee in eloquent relationships

you need create models each table: php artisan make:model board

note: laravel knows pluralize model model board becomes table boards. works words like: copy/copies, etc.

artisan creates migration file each model create.

in migration, need define foreign keys.

schema::create('boards', function(blueprint $table)         {             $table->increments('id');             $table->timestamps();         });   schema::create('standards', function(blueprint $table)         {             $table->increments('id');             $table->integer('board_id')->unsigned();             $table->timestamps();              $table->foreign('board_id')->references('id')->on('boards')->ondelete('cascade');         });  schema::create('subjects', function(blueprint $table)         {             $table->increments('id');             $table->integer('board_id')->unsigned();             $table->integer('standard_id')->unsigned();             $table->timestamps();              $table->foreign('board_id')->references('id')->on('boards')->ondelete('cascade');             $table->foreign('standard_id')->references('id')->on('subjects')->ondelete('cascade');         }); 

etc...

and in each model file, define relationship:

<?php   namespace app;  use illuminate\database\eloquent\model;  class board extends model {      protected $fillable = [];      public function standards()     {         return $this->hasmany('app\standard');     }      public function subjects()     {         return $this->hasmany('app\subject');     }       ...   } 

and in other models:

<?php   namespace app;  use illuminate\database\eloquent\model;  class standard extends model {      protected $fillable = [];      public function board()     {         return $this->belongsto('app\board');     }   }   <?php   namespace app;  use illuminate\database\eloquent\model;  class subject extends model {      protected $fillable = [];      public function board()     {         return $this->belongsto('app\board');     }      public function standard()     {         return $this->belongsto('app\standard');     }      ...   } 

now, in laravel, can like:

board::find(1)->subjects or subject::find(4)->board

hope helps!


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -