【发布时间】:2015-05-29 23:13:44
【问题描述】:
我使用 Laravel 已经五天了,在观看了 Jeffrey Way 几个小时后,我决定深入研究构建一个应用程序来学习。
我被困在使用 hasManyThrough 布局中的表格并识别作为表格之间链接的列的点上。 Eloquent 正在尝试使用名为“id”的列作为它找不到的主键。在我的表中,我使用如下命名约定 tablename_id。在我的类函数中,我指定要使用的列,但它失败并出现错误:
QueryException in Connection.php line 620:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cable_installations.id' in 'on clause' (SQL: select `cable_specifications`.*, `cable_installations`.`cable_specifications_id` from `cable_specifications` inner join `cable_installations` on `cable_installations`.`id` = `cable_specifications`.`cable_installations_id` where `cable_installations`.`cable_specifications_id` is null)
我正在尝试检索:
选择的 cable_installation_method 允许的不同电缆规格列表
谢谢!
表 1:电缆规格
cable_specifications_id (REPEATS)
other_columns...
表 2:cable_installation_methods
cable_installation_methods_id (UNIQUE)
other_columns...
表 3:cable_installations (PIVOT)
cable_specifications_id (REPEATS)
cable_installation_methods_id (REPEATS)
我的课程是:
use Illuminate\Database\Eloquent\Model as Eloquent;
class CableInstallationMethod extends Eloquent {
protected $table = 'cable_installation_methods';
protected $fillable = [];
public function CableInstallation()
{
return $this->hasMany('CableInstallation');
}
public function CableSpecByInstall()
{
return $this->hasManyThrough('App\CableSpecification', 'App\CableInstallation', 'cable_specifications_id', 'cable_installations_id');
}
}
在我的控制器中,我按以下方式调用此函数:
public function VoltageDropLoad()
{
$InstallationMethods = CableInstallationMethod::all();
$CableSelected = CableInstallationMethod::where("cable_installation_methods_id", 1)->first();
$CableTypes = $CableSelected->CableSpecByInstall()distinct()->get()->toJson();
return view('pages.voltcalc', compact('InstallationMethods', 'CableTypes', 'CableTypes'));
}
【问题讨论】:
标签: eloquent laravel-5 has-many-through