laravel 5:4个以上表之间的可伸缩关系


laravel 5 : scalable relationship between more than 4 tables

我正在进行一个电子学习项目,希望在表之间建立一个可扩展的关系,但我坚持如何使用雄辩的关系来映射它们。我有5桌

 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 

关于结构的描述

  • 委员会代表研究委员会是指州委员会和所有
  • 标准代表类示例:1st-2nd等
  • 科目如mathscience
  • 章节就像数学科目的数字系统
  • questiontypes表示这个项目中的问题类型我有三种类型的问题,但它可以更多
  • 问题表根据boardstandardsubject包含本章的所有问题

我使用的是laravel 5,我是雄辩关系的新手

您需要为每个表创建模型:php artisan make:model Board

注意:Laravel知道要将你的模型复数化,这样模型板就变成了桌子板。这也适用于像这样的词:复制/副本,等等

Artisan还会为您创建的每个模型创建一个移植文件。

在这个迁移中,您需要定义外键。

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');
        });

等等。。。

在每个Model文件中,您定义关系:

<?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');
    }

    ...

}

在其他型号中:

<?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');
    }
    ...

}

现在,在Laravel中,您可以执行以下操作:

Board::find(1)->subjectsSubject::find(4)->board

希望这能有所帮助!