添加了奇怪的列名称

时间:2014-04-04 09:35:51

标签: laravel many-to-many eloquent

我有一个'公司' certificaten'和' companies_certificaten'表:

在我的联名表公司_certificaten我有' companies_id'和' certificaten_id'

这是我公司模式的一部分

public function certificaten(){

      return $this->belongstoMany('Certificate', 'companies_certificaten','companies_id');

}

这是我的证书模型的一部分:

public function companies(){

     return  $this->belongstoMany('Company', 'companies_certificaten','certificaten_id');
}

现在我想获得所有证书:

 'certificaten' => Company::find($company_id)->certificaten

这是我得到的完整错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'companies_certificaten.certificate_id' in 'field list' (SQL: select `certificaten`.*, `companies_certificaten`.`companies_id` as `pivot_companies_id`, `companies_certificaten`.`certificate_id` as `pivot_certificate_id` from `certificaten` inner join `companies_certificaten` on `certificaten`.`id` = `companies_certificaten`.`certificate_id` where `companies_certificaten`.`companies_id` = ?) (Bindings: array ( 0 => 48, ))

这部分没有理解错误:

'companies_certificaten.certificate_id' in 'field list'

`companies_certificaten`.`certificate_id` as `pivot_certificate_id`

在我的模型或表中没有任何地方我有" certificate_id"但我有一个" certificaten_id"

laravel在哪里得到这个" certificate_id"从?不是来自我。

1 个答案:

答案 0 :(得分:0)

这是雄辩的默认命名约定,你无处可覆盖它。这将为您完成这项工作:

public function certificaten(){
  return $this->belongstoMany('Certificate', 'companies_certificaten','companies_id', 'certificaten_id');
}

然后你会得到company_id not found错误,所以也要更新这个:

public function companies(){
  return  $this->belongstoMany('Company', 'companies_certificaten','certificaten_id', 'companies_id');
}