【发布时间】:2016-01-16 07:41:18
【问题描述】:
我有一个 cmets 表,其中包含文章、食谱和产品的 cmets。所以它是polymorphic 关系。我的 cmets 表中有两列 rel_id and rel_type 用于此关系。
现在在我的Comment.php 我有以下关系
public function rel()
{
$this->morphTo();
}
在我的其他所有课程中,我都在关注
public function comments()
{
return $this->morphMany('App\Models\Comment', 'rel');
}
当我尝试获取评论及其所有相关数据的所有者时,我发现未找到类错误。例如
$comments = Comment::find(1);
echo $comments->rel_type //article
现在如果我想获取文章的数据以及何时尝试
$comments->rel
我找到了article class not found。我正在使用命名空间App\Models\Article 我已经搜索了它,我找到了here 的答案。当我尝试接受的答案时,没有任何反应,错误保持不变。当我尝试同一问题的第二个答案时,我发现
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
我的最终目标是获取评论所有者数据,例如 $cmets->articles->id 等。请指导我该怎么做?
【问题讨论】:
-
请发布您的数据库布局
标签: laravel laravel-4 namespaces polymorphic-associations