Overriding the Model constructor is fraught. The correct signature is public function __construct(array $attributes = []); but you are not accepting the array of attributes (or passing them to the parent). Is there a reason for the getter; can't you simply set the default attributes on the Model instead???
Eloquent Model set properties during or right after __construction
So I have this complex model that represents a part -- we'll say a computer part. The vendor supplies a limited amount of data that I pull into the database. From there I have written many functions (inside the part model class) that are used to interpret that data. As it stands, the model currently calls the same functions many times as it parses through the data which is causing it to perform more slowly than I would like.
I tried creating new properties on the class and populate them using the __construct method (yes calling the parent first) but the properties refuse to be set. They end up as null. What I'd like to do is accomplish what, in theory, the following should do but doesn't ...
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Exception, Session;
use Illuminate\Http\Request;
class Part extends Model
{
public foo_property; // Getting this property after the model is instantiated results in 'null'
public function __construct()
{
parent::__construct();
$this->foo_property = $this->get_foo_property();
}
public function get_foo_property()
{
return 'Foo';
}
}
This way I can call all of my various methods just once and rely on the properties to perform the rest of my calculations.
What am I missing?
Thanks in advance for any help?
Please or to participate in this conversation.