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

alnouirah's avatar

Add unique composite key addition to primary key !

Hello guys :) i have three table : Companies

id (int) primary
name (varchar)

branches

id (int) primary
company_id (unsigned integer) references (id) on (companies)
name

users

id (int) primary
branch_id (unsigned integer) references (id) on (branches)

The problem is that , i want the name of the branch and the id of the company together to be unique . i searched a lot without figuring out how to do it ! i have tried this :

$table->increments('id');
$table->primary(['player_id', 'team_id']);

but it throw an exception said two primary keys are not allowed . i don't want to delete the increment(id) because i use it in users table . so how do you implement this kind of logic !

0 likes
1 reply
alnouirah's avatar
alnouirah
OP
Best Answer
Level 2

oh, sorry guys i found the answer :

$table->unique(['name', 'agent_id']);

The real problem was in my validation rule i made the branch name unique (stupid) .

Please or to participate in this conversation.