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

AlexMunoz's avatar

Problem with custom primary key and many to many relationship

Hello,

I have three tables: company, sector, company_sector.

Company relevant fields: user_id (primary)

Sector relevant fields: code (primary), name

Company sector relevant fields: id (primary), user_id, name (which is the code in the sector table)

I'm trying to stablish a belongsToMany relationship in the Sector model like this but it's not working:

public function companies(){
    $this->belongsToMany('App\Company', 'company_sector', 'name', 'user_id');
}

I have set up the primary keys in both the Sector and the Company models.

On sector:

protected $primaryKey = 'code';

On company:

protected $primaryKey = 'user_id';

When I try to get the companies of a sector I always get an empty array.

$sector = App\Sector::find('sector_name_here');
$sector->companies;

This is what I get when I print the query logger:

 [
"query" => "select `companies`.*, `company_sector`.`name` as `pivot_name`, `company_sector`.`user_id` as `pivot_user_id` from `companies` inner join `company_sector` on `companies`.`user_id` = `company_sector`.`user_id` where `company_sector`.`name` = ?",
       "bindings" => [
         0,
       ],
]

The query is fine but the binding is empty, I should be seeing the sector code there.

Any ideas on how to resolve this?

Thanks

0 likes
7 replies
jekinney's avatar

Your query is not an eloquent or query builder, it's a raw query. So your relationship in the model will never be used. If you are using straight mysql queries you need to do it as normal.

Other words your question shows code that is irrelevant and I have no idea what you're trying to do.

Maybe watch the laravel fundamental series, if you haven't yet or watch it again if you have as your code is all over the place.

willvincent's avatar

Why are you saying 'name' is a key in your relationship? That's not right...

In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to:

return $this->belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id');

If I'm understanding your table structure properly, your relationship should be defined thusly:

public function companies(){
    $this->belongsToMany('App\Company', 'company_sector', 'code', 'user_id');
}
AlexMunoz's avatar

@willvincent thanks for your response. The problem is that the column on the pivot table is name not code. So company_sector.name contains the sector code. (I know it doesn't make much sense but that's how the database was designed and I can't modify it).

willvincent's avatar

Then it's probably unlikely you're going to be able to use eloquent relationships, and you'll have to instead resort to query builder and joins.

AlexMunoz's avatar
AlexMunoz
OP
Best Answer
Level 4

After further inspection I found that the Sector primary key ('code') was being casted, that's why it was returning a 0 instead of the appropiate value.

Adding this property to the Sector model solved the issue.

public $incrementing = false;

This property must be set when the model is not using an autoincremental primary key.

Thanks @willvincent and @jekinney for your time and help.

4 likes
adrrosales's avatar

You are the Alex Munoz I know? Thank you for the answer, it solved my doubt.

Please or to participate in this conversation.