I have an extra column for usernames in my role_user pivot table, how can I sync the username with the roles ?
role_user pivot table
I need to sync the user name in the name field
This is the update function where I have added the sync
public function update(Request $request, User $user)
{
$role = $request->role;
$userName = Auth::user();
$user->roles()->sync([$role=>['name'=>$userName->name]]);
dd('stop');
}
Here's the relationship in my User Model
public function roles()
{
return $this->belongsToMany(Role::class)->withPivot(['name'])->withTimestamps();
}
Here's the role_user table migration
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');
}
}
I got this error if when I tried

This is what i get when i dump the request and Auth user name
Dump codes
$userName = Auth::user();
dump('Auth user name = '.$userName->name);
dump($request->all());
dd('stop');


$user->nameinstead of$userName->name... since$useris the user for that id you are adding to the pivot table? - lagboxnull? and make sure$request->roleisidof role or it won't work. - Mojtaba Hn