I have a custom setter that I’m running in a __construct
method on my model.
This is the property I’m wanting to set.
protected $directory;
My Constructor
public function __construct() { $this->directory = $this->setDirectory(); }
The setter:
public function setDirectory() { if(!is_null($this->student_id)){ return $this->student_id; }else{ return 'applicant_' . $this->applicant_id; } }
My problem is that inside my setter the, $this->student_id
(which is an attribute of the model being pulled from the database) is returning null
.
When I dd($this)
from inside my setter, I notice that my #attributes:[]
is an empty array.
So, a model’s attributes aren’t set until after __construct()
is fired. How can I set my $directory
attribute in my construct method?
Advertisement
Answer
You need to change your constructor to:
public function __construct(array $attributes = array()) { parent::__construct($attributes); $this->directory = $this->setDirectory(); }
The first line (parent::__construct()
) will run the Eloquent Model
‘s own construct method before your code runs, which will set up all the attributes for you. Also the change to the constructor’s method signature is to continue supporting the usage that Laravel expects: $model = new Post(['id' => 5, 'title' => 'My Post']);
The rule of thumb really is to always remember, when extending a class, to check that you’re not overriding an existing method so that it no longer runs (this is especially important with the magic __construct
, __get
, etc. methods). You can check the source of the original file to see if it includes the method you’re defining.