Reputation: 10408
I'm attempting to create a new attribute for one of my models in a Laravel project, yet whenever I try to do so, I'm given an ErrorException
. Here's my model's code:
class Journey extends Eloquent {
public function getArticleAttribute($value) {
if (is_null($this->article)) { // Exception occurs here
$this->article = file_get_contents($this->article_url);
}
return $this->article;
}
}
and I promptly receive this error:
Undefined property: Journey::$article
on the line highlighted in my above code.
Well, of course it's not defined! That's why I'm trying to create it! This should be super simple, yet it's just not working. Any ideas? $this->article_url
is defined and exists, so the problem isn't with that...
Upvotes: 0
Views: 382
Reputation: 4769
If "article" is not a column of your db table, you should define it as class attribute:
class Journey extends Eloquent {
protected $article = null;
public function getArticleAttribute($value) {
if (is_null($this->article)) { // Exception occurs here
$this->article = file_get_contents($this->article_url);
}
return $this->article;
}
}
Upvotes: 1