The question might be a bit odd and might seem nonsensical. Let me elaborate, On my DB there is a Roles table. This has 4 default roles. These roles are the basic defaults, though the users could add custom ones. Anyway, I digress.
During a registration process I pull down these default values with this function:
public function getDefaultRoles() {
$defaultRoles = Role::where('company_id', 0)->where('id', '!=', 1)->get();
return ($defaultRoles)->toArray();
}
Then I try to set the newly created company's id like so:
public function setCompanyRoles($companyId) {
foreach($this->getDefaultRoles() as $role) {
$newRole = new Role($role);
$newRole->company_id = $companyId;
$newRole->save();
}
}
Now here is where to problem is, if I try to use the above code it gives me the following error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2' for key 'PRIMARY' (SQL: insert into roles
Thank you for reading my post, hope it makes sense. Thank you in advance!