Laravel中迁移外键与雄辩的关系 [英] Migration Foreign Key Vs Eloquent Relationships in Laravel

查看:79
本文介绍了Laravel中迁移外键与雄辩的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

1)在迁移"表中定义外键.

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

我已经阅读了文档,但仍然对以下内容感到困惑:

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

  1. 我需要同时使用两者还是只需要1个?

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

同时使用两者是否有误?还是做到了 多余或引起冲突?

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

使用雄辩的关系有什么好处,而无需提及 迁移列中的外键?

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

有什么区别?

这些是我现在拥有的代码.对于我是否仍需要清除在迁移文件中设置的外键,我仍然不清楚.

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.

迁移:

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

具有雄辩关系的模型:

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

有人可以帮我理解吗?

推荐答案

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

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.

因此,一旦您在一个模型中编写了hasMany,并在另一个模型中编写了相应的函数,则只有您的模型相互了解,然后您才能在模型中以及在模型中成功查询事物.您的数据库.

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.

还请注意,如果通过模型中的hasManybelongsTo正确定义了关系,但未在belongsTo其他表的模型表中提供外键,则您的关系将无法正常工作

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.

简而言之,两者都是强制性的.

In short, both are equally compulsory.

这篇关于Laravel中迁移外键与雄辩的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆