【问题标题】:Laravel 5.0 hasManyThrough column not found issueLaravel 5.0 hasManyThrough 列未找到问题
【发布时间】: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


    【解决方案1】:

    根据您的 CableInstallationMethod 类,您可能错过了为模型定义主键字段:

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

    【讨论】:

    • 太棒了,这真的很有帮助!谢谢!
    猜你喜欢
    • 2015-05-29
    • 2020-03-07
    • 2017-02-07
    • 2016-06-23
    • 1970-01-01
    • 2015-07-05
    • 2015-10-31
    • 2020-04-12
    • 1970-01-01
    相关资源
    最近更新 更多