Trying to seed a database with users that have many roles. This is a many-to-many relationship. Why is the roleid column not being populated and why am I receiving this error:
P.S. I’m aware my table naming conventions are incorrect.
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'roleid' cannot be null (SQL: insert into `user_role` (`userid`, `videoid`) values (9, ?), (6, ?))
User Model:
public function role() {
return $this->belongsToMany(Video::class, 'user_roll', 'userid', 'roleid')->withTimestamps();
}
Role Model:
public function user() {
return $this->belongsToMany(Actress::class, 'user_role', 'roleid', 'userid');
}
Pivot Table
public function up()
{
Schema::create('user_role', function (Blueprint $table) {
$table->foreignId('userid')->constrained('users','userid')->cascadeOnDelete();
$table->foreignId('roleid')->constrained('roles','roleid')->cascadeOnDelete();
$table->timestamps();
});
}
Seeder:
foreach (Role::all() as $role) {
$users= User::inRandomOrder()->take(rand(1,3))->pluck('userid');
$video->user()->attach($users);
}