In Laravel 5.1 I can see that table column relationships can be set-up in 2 ways:

在Laravel 5.1中,我可以看到表列关系可以通过两种方式设置:

1) Defining Foreign Keys in the Migration table.

1)在迁移表中定义外键。

2) Defining the Eloquent relationships in the Models.

2)定义模型中的雄辩关系。

I have read the documentations and I am still confused on the following:

我已阅读文件,我仍然对以下内容感到困惑:

  1. Do I need to use both or only 1 is needed?

    我是否需要同时使用或仅需要1个?

  2. Is it wrong to use both at the same time? Or does it make it redundant or cause conflicts?

    同时使用两者是不对的吗?或者它是否使其多余或引起冲突?

  3. What is the benefit of using Eloquent relationships without mentioning the Foreign keys in migration column?

    使用Eloquent关系而不提及迁移列中的外键有什么好处?

  4. What is the difference?

    有什么不同?

These are the codes I have now. Its still unclear to me if I need to remove the foreign keys I have set-up in my migration file.

这些是我现在的代码。如果我需要删除我在迁移文件中设置的外键,我仍然不清楚。

Migration:

移民:

  public function up()
    {   

       Schema::create('apps', function (Blueprint $table) {
          $table->increments('id');
          $table->string('app_name');
          $table->string('app_alias');
          $table->timestamps();
          $table->engine = 'InnoDB';
       });

      // This is the second Migration table
      Schema::create('app_roles', function (Blueprint $table) {
          $table->increments('id');
          $table->integer('app_id')->unsigned()->index();
          $table->integer('user_id')->unsigned()->index();
          $table->integer('role_id')->unsigned()->index();
          $table->engine = 'InnoDB';

          $table->unique(array('app_id', 'user_id'));

          $table->foreign('app_id')
                ->references('id')
                ->on('apps')
                ->onDelete('cascade');

          $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

          $table->foreign('role_id')
                ->references('id')
                ->on('roles')
                ->onDelete('cascade');
        });     
    }

Model with Eloquent Relationships:

具有雄辩关系的模型:

// App Model
class App extends Model
{

     public function appRoles() {
         return $this->hasMany('App\Models\AppRole');
     }
}

// AppRole Model
class AppRole extends Model
{
   public function app() {
       return $this->belongsTo('App\Models\App');
   }

   public function user() {
       return $this->belongsTo('App\User');
   }

   public function role() {
       return $this->belongsTo('App\Models\Role');
   }
}

// User Model
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    .....
    public function appRole() {
         return $this->belongsToMany('App\Models\AppRole');
     }
}

// Role Model
class Role extends EntrustRole
{
     public function appRole() {
         return $this->hasMany('App\Models\AppRole');
     }
}

Can someone help me understand this please?

有人可以帮我理解这个吗?

2 个解决方案

#1


19

Both go hand in hand. One is in-complete without the other one. If you want your relations to work properly, you need to define both of these things.

两者齐头并进。一个是完整的而没有另一个。如果您希望关系正常工作,则需要定义这两个方面。

If you have just defined the foreign key in a migration file, the relation would work just in case you write a raw query. It won't work on your models since, you haven't written anything about relations in your models.

如果您刚刚在迁移文件中定义了外键,则只要您编写原始查询,该关系就会起作用。它不适用于您的模型,因为您没有在模型中写过关于关系的任何内容。

So, as soon as you write hasMany in one of your models, and corresponding function in the other model, only then your models know about each other, and then you can successfully query things through your model as well as in your database.

因此,只要您在其中一个模型中编写hasM​​any,并在另一个模型中编写相应的函数,只有您的模型才能相互了解,然后您才能通过模型​​和数据库成功查询。

Also note that if you have properly defined relations through hasMany and belongsTo in your models, but haven't provided foreign key in the table of the model who belongsTo other table, your relations won't work.

另请注意,如果您在模型中通过hasMany和belongsTo正确定义了关系,但是没有在属于其他表的模型表中提供外键,则您的关系将无法正常工作。

In short, both are equally compulsory.

简而言之,两者都是同等的义务。

更多相关文章

  1. mysql-5.5配置主从 及 主主关系
  2. 如何在MySQL中创建关系
  3. MySQL是一个非常流行的小型关系型数据库管理系统
  4. 有没有一种方法可以在不破坏外键依赖关系的情况下将MySQL数据库
  5. 从模型到控制器并返回到模型的数据
  6. 装机建项目vs2017和mysql5.7下建项目用EF建实体模型的过程..
  7. 内部联接如何使用Doctrine和Symfony2处理多对多关系
  8. 创造一个多态关系与教义
  9. mysql中 character set 和collation关系

随机推荐

  1. 【Android Studio】解决adb not respondi
  2. Android初级第一讲---Android开发环境的
  3. 如何创建一个四位数的密码Android布局
  4. 微信小程序开发环境搭建
  5. 七牛---Android SDK断点续传与暂停上传De
  6. 编译Android4.3内核源代码
  7. 使用片段活动创建的ADT空白活动。
  8. 关于android的webview打开淘宝天猫链接问
  9. Android学习之Asynctask异步操作
  10. android 文件读取(assets)