Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A __construct on an Eloquent Laravel Model

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?

like image 988
Nathan Lochala Avatar asked May 28 '15 09:05

Nathan Lochala


People also ask

What is __ construct in Laravel?

The constructor is a method that is called whenever a new instance of a (PHP) class is instantiated. This is object oriented programming 101. The code you shared will apply the auth:instructor middleware on all controller actions (in that class) except the action called attendanceShowToStudent .

What is __ construct in PHP?

PHP - The __construct FunctionA constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Notice that the construct function starts with two underscores (__)!

What is Laravel eloquent model?

Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table.

What is eloquent builder in Laravel?

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.


2 Answers

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.

like image 163
alexrussell Avatar answered Sep 28 '22 06:09

alexrussell


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
like image 39
Jed Lynch Avatar answered Sep 28 '22 06:09

Jed Lynch