迁移外键与Laravel中雄辩的关系

Nee*_*eel 26 php mysql laravel eloquent laravel-5

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

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

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

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

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

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

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

  4. 有什么不同?

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

移民:

  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');
        });     
    }
Run Code Online (Sandbox Code Playgroud)

具有雄辩关系的模型:

// 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');
     }
}
Run Code Online (Sandbox Code Playgroud)

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

Ars*_*Ali 29

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

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

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

还要注意,如果你已经通过hasManybelongsTo在你的模型中正确定义了关系,但是没有在模型表的belongsTo其他表中提供外键,你的关系将无法正常工作.

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

  • 我知道这是一个旧答案,但是不确定此后是否有任何更改。我已经在模型中声明了关系,但是没有在迁移中进行外键约束,但是我的工作仍然很好。雄辩的假设列的名称(例如app_id等)。在这种情况下-是否需要在迁移中指定外键?如果我仍然声明它,它将对我的应用程序增加什么? (2认同)

ape*_*x39 5

Eloquent 根据模型名称假定关系的外键。在这种情况下,App模型被自动假定为具有app_id外键,因此在您的迁移中您不需要指定:

$table->foreign('app_id')
->references('id')
->on('apps')
->onDelete('cascade');
Run Code Online (Sandbox Code Playgroud)

文档