【发布时间】:2015-05-29 22:19:15
【问题描述】:
我使用 Laravel 已经五天了,在观看了 Jeffrey Way 几个小时后,我决定深入研究构建一个应用程序来学习。我被困在使用 hasManyThrough 布局和未找到的类中的表上。任何帮助表示赞赏。
我正在尝试检索:
选择的 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 CableInstallation extends Eloquent {
protected $table = 'cable_installations';
protected $fillable = [];
public function cable_installation_method()
{
return $this->belongsTo('CableInstallationMethod');
}
public function cable_specification()
{
return $this->belongsToMany('CableSpecifications');
}
public function voltage_drop()
{
return $this->belongsToMany('CableVoltageDrop');
}
}
class CableInstallationMethod extends Eloquent {
protected $table = 'cable_installation_methods';
protected $fillable = [];
public function CableInstallation()
{
return $this->hasMany('CableInstallation');
}
public function CableSpecByInstall()
{
return $this->hasManyThrough('CableSpecification', 'CableInstallation', 'cable_specifications_id', 'cable_installations_id')
->distinct();
}
}
class CableSpecification extends Eloquent {
protected $table = 'cable_specifications';
protected $fillable = [];
public function CableInstallFromSpec()
{
return $this->hasMany('CableInstallation');
}
}
在我的控制器中,我按以下方式调用此函数:
public function VoltageDropLoad()
{
$InstallationMethods = CableInstallationMethod::all();
$CableSelected = CableInstallationMethod::where("cable_installation_methods_id", 1)->firstOrFail();
$CableTypes = $CableSelected->CableSpecByInstall()->toJson();
return view('pages.voltcalc', compact('InstallationMethods', 'CableTypes', 'CableTypes'));
}
我最终得到了这个错误:
Model.php 第 911 行中的 FatalErrorException: 未找到“CableInstallation”类
【问题讨论】:
标签: php laravel-5 has-many-through