marked-down
marked-down

Reputation: 10408

Laravel model is telling me a property I'm trying to create does not exist?

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

Answers (1)

Fabrizio D'Ammassa
Fabrizio D'Ammassa

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

Related Questions