Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Berwyn's avatar

How to grab a set of data from database, then duplicate them with different foreign keys?

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!

0 likes
6 replies
Berwyn's avatar

Fixed it, although if anyone has any better suggestion I am listening.

The solution:

public function getDefaultRoles() {
        $defaultRoles = Role::where('company_id', 0)->where('id', '!=', 1)->get();
        foreach($defaultRoles as $role) {
            $role->id = '';
        }
        return ($defaultRoles)->toArray();
    }

Basically, I iterate through the default roles and remove their IDs, these then get auto generated at the setting stage.

CorvS's avatar
CorvS
Best Answer
Level 27

@berwyn Simply exclude the id when creating the new roles:

foreach($this->getDefaultRoles() as $role) {
    $newRole = new Role(Arr::except($role, 'id'));
    $newRole->company_id = $companyId;
    $newRole->save();
}
1 like
Berwyn's avatar

That's really clean chief, thanks!

1 like
MichalOravec's avatar

Just select columns which you need.

public function getDefaultRoles()
{
    return Role::where('company_id', 0)
        ->where('id', '!=', 1)
        ->get(['name', 'company_id', 'other-column'])
        ->toArray();
}

public function setCompanyRoles($companyId)
{
    $roles = data_set($this->getDefaultRoles(), '*.company_id', $companyId);

    Role::insert($roles);
}
1 like
Berwyn's avatar

Thanks, this is another great solution.

Please or to participate in this conversation.