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