Laravel 将用户名同步到 pivot table
Laravel Sync username into pivot table
我的 role_user
数据透视表 table 中有一个额外的用户名列,如何将用户名与角色同步?
role_user
枢轴 table
我需要同步name
字段中的用户名
这是我添加了 sync
的更新功能
public function update(Request $request, User $user)
{
$role = $request->role;
$userName = Auth::user();
$user->roles()->sync([$role=>['name'=>$userName->name]]);
dd('stop');
}
这是我 User Model
中的关系
public function roles()
{
return $this->belongsToMany(Role::class)->withPivot(['name'])->withTimestamps();
}
这是 role_user
table 迁移
class CreateRoleUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('user_id');
$table->string('name')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('role_user');
}
}
如果我尝试时出现此错误
这是我转储请求和 Auth 用户名时得到的结果
转储代码
$userName = Auth::user();
dump('Auth user name = '.$userName->name);
dump($request->all());
dd('stop');
您需要一个由角色 ID 键控的关联数组,每个元素都有一个数组,以便将内容添加到枢轴 table。目前你只有一个角色 ID 数组,这对于 sync
是很好的,但如果你还想设置额外的数据透视表则不行。你需要一个像这样的数组:
[1 => ['name' => ...], 2 => ['name' => ...]]
您可以使用 array_fill_keys
构建此数组,键是角色 ID,值是包含 'name' 字段的数组:
$roles = (array) $request->input('role', []);
$forSync = array_fill_keys($roles, ['name' => $userName->name]);
好的,你几乎做对了。除了..
你需要这样写(使用Collection mapWithKeys方法):
$roles = collect($request->role)->mapWithKeys(function($role){
return [$role => ['name' => Auth::user()->name];
})->toArray();
$user->roles()->sync($roles);
这样做的原因是您需要为每个角色分别指定枢轴table参数。所以你需要 [1 => ['name' => 'John'],2 => ['name' => 'John']]
而不是 [[1,2] => ['name' => 'John']]
才能工作。
我的 role_user
数据透视表 table 中有一个额外的用户名列,如何将用户名与角色同步?
role_user
枢轴 table
我需要同步name
字段中的用户名
这是我添加了 sync
public function update(Request $request, User $user)
{
$role = $request->role;
$userName = Auth::user();
$user->roles()->sync([$role=>['name'=>$userName->name]]);
dd('stop');
}
这是我 User Model
public function roles()
{
return $this->belongsToMany(Role::class)->withPivot(['name'])->withTimestamps();
}
这是 role_user
table 迁移
class CreateRoleUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('user_id');
$table->string('name')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('role_user');
}
}
如果我尝试时出现此错误
这是我转储请求和 Auth 用户名时得到的结果
转储代码
$userName = Auth::user();
dump('Auth user name = '.$userName->name);
dump($request->all());
dd('stop');
您需要一个由角色 ID 键控的关联数组,每个元素都有一个数组,以便将内容添加到枢轴 table。目前你只有一个角色 ID 数组,这对于 sync
是很好的,但如果你还想设置额外的数据透视表则不行。你需要一个像这样的数组:
[1 => ['name' => ...], 2 => ['name' => ...]]
您可以使用 array_fill_keys
构建此数组,键是角色 ID,值是包含 'name' 字段的数组:
$roles = (array) $request->input('role', []);
$forSync = array_fill_keys($roles, ['name' => $userName->name]);
好的,你几乎做对了。除了..
你需要这样写(使用Collection mapWithKeys方法):
$roles = collect($request->role)->mapWithKeys(function($role){
return [$role => ['name' => Auth::user()->name];
})->toArray();
$user->roles()->sync($roles);
这样做的原因是您需要为每个角色分别指定枢轴table参数。所以你需要 [1 => ['name' => 'John'],2 => ['name' => 'John']]
而不是 [[1,2] => ['name' => 'John']]
才能工作。