Laravel 5.0 hasManyThrough 列未找到问题

Laravel 5.0 hasManyThrough column not found issue

我已经进入 Laravel 五天了,在观看 Jeffrey Way 数小时后,我决定深入研究构建一个应用程序来学习。

我被困在使用 hasManyThrough 布局中的表并识别作为表之间链接的列的点上。 Eloquent 正在尝试使用名为“id”的列作为它找不到的主键。在我的表中,我使用如下命名约定 tablename_id。在我的 class 函数中,我指定要使用的列,但失败并显示错误:

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

允许的不同电缆规格列表

谢谢!

TABLE 1: cable_specifications

cable_specifications_id (REPEATS)
other_columns...

TABLE 2: cable_installation_methods

cable_installation_methods_id (UNIQUE)
other_columns...

TABLE 3:cable_installations(枢轴)

cable_specifications_id (REPEATS)
cable_installation_methods_id (REPEATS)

我的 class 是:

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

根据您的 CableInstallationMethod class,您可能没有定义模型的主键字段:

use Illuminate\Database\Eloquent\Model as Eloquent;

class CableInstallationMethod extends Eloquent {

    protected $table = 'cable_installation_methods';

    /* Need to define $primaryKey column here, normally defaults to 'id' */
    protected $primaryKey = 'cable_installation_methods_id';

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

}

设置主键后,您还可以利用 Model::find($id) 而不是 Model:where(...)->first()

public function VoltageDropLoad()
{
    $InstallationMethods = CableInstallationMethod::all();

    $CableSelected = CableInstallationMethod::find(1);

    $CableTypes = $CableSelected->CableSpecByInstall()->distinct()->get()->toJson();

    return view('pages.voltcalc', compact('InstallationMethods', 'CableTypes', 'CableTypes'));
}