【发布时间】:2014-08-08 19:57:53
【问题描述】:
我试图通过$post->file->url 访问文件表上的 url 属性,但我得到 “尝试获取非对象的属性” 错误。我的表和模型不同,因为我不能使用 File 作为模型名称。有什么遗漏吗?我希望有人提出解决方案。这是我的桌子:
- posts:
[PK] id
title
[FK] featured_image
- files:
[PK] id
title
url
我的模特:
class TheFile extends \Eloquent
{
protected $table = 'files';
public function post() {
return $this->belongsTo('Post');
}
}
class Post extends \Eloquent
{
protected $table = 'posts';
public function file() {
return $this->hasOne('TheFile', 'id', 'featured_image');
}
}
提前致谢。
【问题讨论】:
标签: php laravel orm laravel-4 eloquent