【发布时间】:2019-02-16 21:01:30
【问题描述】:
我有两个通过 hasMany 和 belongsTo 方法连接的类。
class InquiryParameter extends Model
{
public function translations()
{
return $this->hasMany(InquiryParameterTranslation::class);
}
}
class InquiryParameterTranslation extends Model
{
public function __construct($inquiry_parameter_id, $language_code, $name, $description)
{
$this->inquiry_parameter_id = $inquiry_parameter_id;
$this->language_code = $language_code;
$this->name = $name;
$this->description = $description;
}
}
但是,当我创建新对象时
$inquiry_parameter = new InquiryParameter;
然后调用方法翻译。
$names = $inquiry_parameter->translations;
我收到错误:
类型错误:函数的参数太少 App\InquiryParameterTranslation::__construct(), 0 传入 /Users/SouL-MAC/Code/konfig/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php 在第 653 行,预计正好 4 行(查看: /Users/SouL-MAC/Code/konfig/resources/views/admin/inquiry/parameters.blade.php)
是否可以与包含构造函数的类使用雄辩的关系?还是我做错了什么?
感谢您的回复
【问题讨论】:
-
我认为你的模型构造函数中需要
parent::__construct(...) -
顺便说一句:这似乎是错误的:
$inquiry_parameter = new InquiryParameter;。应该是这样的:$inquiry_parameter = new InquiryParameter($inquiry_parameter_id, $language_code, $name, $description); -
InquiryParameterTranslation,我还没有 InquiryParameter 类的构造函数
标签: php laravel relationship laravel-5.5 has-many