lundi 16 mai 2016

Using relationship in constructor in Laravel

I have model like this:

class Question extends Model {

    protected $factory;   

    public function type() {
        return $this->belongsTo('App\Models\QuestionType', 'type_id');
    }
}

$factory attribute is resolved based on QuestionType. How can i do that in constructor? When I try something like this

public function __construct($attributes = array())  {
      parent::__construct($attributes);
      $this->createFactory($this->type->name);        
}

I get ErrorException with message Trying to get property of non-object

How can I achieve this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire