laravel 5 : scalable relationship between more than 4 tables

303 Views Asked by At

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

1

There are 1 best solutions below

4
On BEST ANSWER

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.

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, you define the 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 the 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, you can do something like:

Board::find(1)->subjects or Subject::find(4)->board

Hope this helps!