首页 文章

Laravel 5雄辩的关系 - 虽然有很多

提问于
浏览
2

我有三个型号 .

  • 学习

  • 网站

  • 单位

研究具有并属于许多站点,并且属于Study的每个站点都具有并属于许多单元 . 请参阅下图 .

http://tinypic.com/r/ojhx0g/8

我是如何使用Laravel 5 Eloquent Relationships实现这一目标的 .

2 回答

  • 2

    听起来你在Study-> Site和Site-> Unit之间有多对多的关系 . 您可以阅读有关many-to-many relationships here.的Laravel文档

    型号

    以下是Eloquent识别关系所需的相关功能 .

    class Study extends Model {
        // If you named your table differently (like 'studies'), specify that here
        protected $table = 'studys';
    
        // This assumes a pivot table named 'site_study', if you named yours
        // differently you can pass in into the belongsToMany() function as the
        // second parameter.
        public function sites() {
            return $this->belongsToMany('App\Site');
        }
    }
    
    class Site extends Model {
        protected $table = 'sites';
    
        public function studies() {
            return $this->belongsToMany('App\Study');
        }
    
        public function units() {
            return $this->belongsToMany('App\Unit');
        }
    }
    
    class Unit extends Model {
        protected $table = 'units';
    
        public function sites() {
            return $this->belongsToMany('App\Site');
        }
    }
    

    然后,要访问属于研究的网站,您可以执行以下操作:

    $sites = Study::find(1)->sites;
    

    数据透视表迁移

    Laravel希望数据透视表的名称类似于 'alpha_beta' ,其中 alphabeta 是按字母顺序排列的奇异模型名称 . 因此,您对数据透视表的迁移将如下所示:

    class CreateSiteStudyTable extends Migration {
        public function up() {
            Schema::create('site_study', function(Blueprint $table)) {
                $table->integer('site_id')->unsigned();
                $table->foreign('site_id')->references('id')->on('sites');
                $table->integer('study_id')->unsigned();
                $table->foreign('study_id')->references('id')->on('studys'); // or whatever you named it
                $table->unique(['site_id', 'study_id']);
                $table->timestamps();
            });
        }
    
        public function down() {
            Schema::drop('site_study');
        }
    }
    
    class CreateSiteUnitTable extends Migration {
        public function up() {
            Schema::create('site_unit', function(Blueprint $table)) {
                $table->integer('site_id')->unsigned();
                $table->foreign('site_id')->references('id')->on('sites');
                $table->integer('unit_id')->unsigned();
                $table->foreign('unit_id')->references('id')->on('units');
                $table->unique(['site_id', 'unit_id']);
                $table->timestamps();
            });
        }
    
        public function down() {
            Schema::drop('site_unit');
        }
    }
    

    你可以阅读Foreign Keys in Laravel here.

  • 0

    你必须创建3个模型学习,网站和单元,你的图表研究有很多网站和网站有很多单位,在你的图表研究没有直接关系单位你的雄辩模型将是这样的 .

    class Study extends Model {
        public function sites(){
            return $this->hasMany('App\Site');
        }
    }
    
    class Site extends Model {
    
        public function units(){
            return $this->hasMany('App\Unit');
        }
    
        public function study(){
            return $this->belongsTo('App\Study');
        }
    
    }
    
    class Unit extends Model {
    
        public function sites(){
            return $this->belongsTo('App\Site');
        }
    
    }
    

相关问题