【问题标题】:Laravel 5 hasManyThrough issueLaravel 5 hasManyThrough 问题
【发布时间】: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


    【解决方案1】:

    您实际上还没有取回您的“电缆”。试试:

    $CableSelected = CableInstallationMethod::where("cable_installation_methods_id", 1)->firstOrFail();
    

    只是为了澄清一点。在这种情况下,您的电缆安装并不是真正的枢纽(根据我的估计)。它只是另一个包含与其他两个关系的模型。数据透视表通常没有模型,用于多对多关系。

    例如;类似cable_installers for cable_installations 的东西,您可以让多个安装人员进行电缆安装。在这种情况下,“installers”将是一个模型(带有一个表格),“installations”将是一个模型(已经提供了表格),installations_installers 将是一个存储关系的数据透视表。

    【讨论】:

    • 嗨@Scopey 感谢您的回复。我添加了“->get()”,但在 PagesController.php 第 25 行中的“FatalErrorException”的“hasManyThrough”函数上仍然出现错误:调用未定义的方法 Illuminate\Database\Eloquent\Collection::CableSpecByInstall()"
    • 对不起,我的错 - 它返回一个“集合”,它本质上是一个美化的结果数组。使用->firstOrFail() 而不是->get()。我会更新我的答案。如果您希望它在没有与您的查询匹配的情况下不引发异常,则可以使用->first()
    • 嗨@Scopey,感谢您的第二次回复!当我进行您建议的更改时,代码似乎已经转向“找不到类”问题。
    猜你喜欢
    • 1970-01-01
    • 2020-03-07
    • 2016-02-11
    • 2017-02-07
    • 2015-05-29
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    相关资源
    最近更新 更多