Laravel、主元 table 与多对多关系冲突
Laravel, pivot table conflicts with many-to-many relationship
大家好,我正在使用 spatie 二进制 uuid 包,我几乎没有怀疑
目前所做的事情:
User.php 迁移:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->uuid('uuid');
$table->primary('uuid');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
角色迁移只有名为 "name" 的基本字段和时间戳
枢轴table:role_user
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->unsigned()->nullable()->index();
$table->uuid('user_uuid');
});
}
我知道这是非常错误的,我不知道该怎么办,我正在尝试通过此调用保存角色模型
$uuid = '478d7068-ae64-11e8-a665-2c600cf6267b';
$model = User::withUuid($uuid)->first();
$model->roles()->save(new Role(['name' => 'Admin']));
它不起作用,我哪里出错了?我认为这与 role_user 迁移
有关
User.php
public function roles()
{
return $this->belongsToMany(Role::class);
}
试试这个,枢轴迁移:
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->unsigned()->nullable()->index();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->uuid('user_uuid');
$table->foreign('user_uuid')->references('uuid')->on('users')->onDelete('cascade');
});
}
角色关系:
public function roles(){
return $this->belongsToMany(Role::class,'role_user','user_uuid','role_id');
}
如果不起作用请告诉我
你应该编辑你与此的关系
return $this->belongsToMany(Role::class,'role_user','user_uuid','role_id');
如果您说出您的错误,我们可以更好地帮助您
大家好,我正在使用 spatie 二进制 uuid 包,我几乎没有怀疑 目前所做的事情: User.php 迁移:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->uuid('uuid');
$table->primary('uuid');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
角色迁移只有名为 "name" 的基本字段和时间戳
枢轴table:role_user
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->unsigned()->nullable()->index();
$table->uuid('user_uuid');
});
}
我知道这是非常错误的,我不知道该怎么办,我正在尝试通过此调用保存角色模型
$uuid = '478d7068-ae64-11e8-a665-2c600cf6267b';
$model = User::withUuid($uuid)->first();
$model->roles()->save(new Role(['name' => 'Admin']));
它不起作用,我哪里出错了?我认为这与 role_user 迁移
有关User.php
public function roles()
{
return $this->belongsToMany(Role::class);
}
试试这个,枢轴迁移:
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->unsigned()->nullable()->index();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->uuid('user_uuid');
$table->foreign('user_uuid')->references('uuid')->on('users')->onDelete('cascade');
});
}
角色关系:
public function roles(){
return $this->belongsToMany(Role::class,'role_user','user_uuid','role_id');
}
如果不起作用请告诉我
你应该编辑你与此的关系
return $this->belongsToMany(Role::class,'role_user','user_uuid','role_id');
如果您说出您的错误,我们可以更好地帮助您