Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel model return Undefined property::$attribute

Tags:

laravel

model

I have model Post with attributes: id, content, image, publish_date. In the Post model I can get all attributes but image when using $this->$attr it shows me Undefined property when calling $this->image. I don't know why. But when I call in the Controller: Post::find(1)->image, it works fine. Can anyone help me pls?

like image 807
Ngan Nguyen Avatar asked Dec 25 '17 08:12

Ngan Nguyen


2 Answers

Since you're using an accessor, try to do this:

public function getImageAttribute($value)
{
    dd($value);
}
like image 58
Alexey Mezenin Avatar answered Nov 11 '22 17:11

Alexey Mezenin


This was the first link that showed up in my google search, so I am posting the solution I found here as well, for anyone else running into the same issue.

In relation to the question above, I would have had to use $this->attributes['image'], specifically for accessing.

like image 43
tr00ks Avatar answered Nov 11 '22 17:11

tr00ks