繁体   English   中英

Laravel 如何在两个 Seeder 之间建立关系

[英]Laravel How to create a relationship between two Seeders

在我的laravel应用程序中,我在数据库中有两个表:

- education_categories
- educations

现在,我想创建两个与它们之间的关系的 Seeder。

EductionCategory 播种机:

public function run()
{
    DB::table('education_categories')->insert([
        'title' => "IT"
    ]);

    DB::table('education_categories')->insert([
        'title' => "Nature and science"
    ]);

    DB::table('education_categories')->insert([
        'title' => "Science"
    ]);

    DB::table('education_categories')->insert([
        'title' => "Construction"
    ]);

    ...etc
}

现在我想要一个与 Educations Category Seeder 有某种关系的 Educations Seeder。 就像是:

DB::table('educations')->insert([
    'title' => "Programmer",
    'education_categories_id' => 1
]);

这个对吗? 我是否必须在我的education_categories Model 中创建hasMany关系?

在 CLI 中使用php artisan make:model ModelName命令制作两个不同的模型,并创建如下关系:

class TestRelationship extends Model {

public function testrelation()
  {
    return $this->hasMany('path\TestBelongs');
  }
}



  class TestBelongs extends Model {

    public function belongsme()
    {
      return $this->belongsTo('path\TestRelationship');
    }

  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM