【问题标题】:Strange column name added添加了奇怪的列名
【发布时间】:2014-04-04 09:35:51
【问题描述】:

我有一个 'companies'、'certificaten' 和 'companies_certificaten' 表:

在我的联接表 Companies_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”?不是我的。

【问题讨论】:

    标签: laravel many-to-many eloquent


    【解决方案1】:

    这是 Eloquent 的默认命名约定,你无法覆盖它。这将为您完成工作:

    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');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-09
      • 2018-07-26
      • 2011-12-14
      • 1970-01-01
      • 2012-07-19
      • 2016-03-27
      • 2020-02-17
      • 1970-01-01
      相关资源
      最近更新 更多