我在我的模型的__construct
方法中运行了一个自定义的setter。
这是我想要设置的属性。
protected $directory;
我的构造函数
public function __construct()
{
$this->directory = $this->setDirectory();
}
设置器:
public function setDirectory()
{
if(!is_null($this->student_id)){
return $this->student_id;
}else{
return 'applicant_' . $this->applicant_id;
}
}
我的问题是,在我的 setter 内部,从数据库中获取的模型的属性 $this->student_id
返回了 null
。 当我在 setter 中使用 dd($this)
时,我注意到我的 #attributes: []
是一个空数组。因此,模型的属性直到触发 __construct()
方法之后才会被设置。如何在我的构造方法中设置 $directory
属性?