A __construct on an Eloquent Laravel Model A __construct on an Eloquent Laravel Model laravel laravel

A __construct on an Eloquent Laravel Model


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.


I wouldn't ever use a constructor in eloquent. Eloquent has ways to accomplished what you want. I would used a boot method with an event listener. It would look something like this.

protected static function boot(){    parent::boot();    static::retrieved(function($model){         $model->directory = $model->student_id ?? 'applicant_' . $model->applicant_id;    });}   

Here are all the model events you can use...

  • retrieved
  • creating
  • created
  • updating
  • updated
  • saving
  • saved
  • deleting
  • deleted
  • restoring
  • restored