我有一个自定义setter,我正在__construct我的模型上的方法中运行.
这是我想要设置的属性.
protected $directory;
Run Code Online (Sandbox Code Playgroud)
我的构造函数
public function __construct()
{
$this->directory = $this->setDirectory();
}
Run Code Online (Sandbox Code Playgroud)
二传手:
public function setDirectory()
{
if(!is_null($this->student_id)){
return $this->student_id;
}else{
return 'applicant_' . $this->applicant_id;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,在我的setter里面,$this->student_id(这是从数据库中提取的模型的属性)正在返回null.当我dd($this)来自我的二传手时,我注意到我的#attributes:[]是一个空阵列.
因此,模型的属性在__construct()触发之后才会设置.如何$directory在构造方法中设置属性?