Laravel模型返回未定义属性:: $ attribute

Nga*_*yen 4 model laravel

我的模型Post具有以下属性:id, content, image, publish_date。在Post模型中,我可以获取所有属性,但使用image时$this->$attr,调用$ this-> image 时会显示Undefined属性。我不知道为什么 但是当我致电时Controller: Post::find(1)->image,它可以正常工作。有人可以帮我吗?

小智 6

这是我的谷歌搜索中出现的第一个链接,所以我也发布了我在这里找到的解决方案,供其他遇到相同问题的人使用。

关于上述问题,我将不得不使用$this->attributes['image'],专门用于访问。


Ale*_*nin 5

由于您使用的是accessor,请尝试执行以下操作:

public function getImageAttribute($value)
{
    dd($value);
}
Run Code Online (Sandbox Code Playgroud)