在laravel 5.2中建立多态关系

在laravel 5.2中建立多态关系,laravel,laravel-5.2,Laravel,Laravel 5.2,答案有评论,评论有投票。 答案有投票权 votes table: id user_id vote votable_id votable_type 1 1 5 1 Comment 2 1 6 1 Post ... 发布模型: public function comments() { return $this->hasMany('App\Comment','post_

答案有评论,评论有投票。
答案有投票权

votes table:
id  user_id  vote  votable_id  votable_type
1     1       5     1          Comment
2     1       6     1          Post
...
发布模型:

 public function comments()
    {
        return $this->hasMany('App\Comment','post_id','id'); 
    }

   public function votes()
    {
        return $this->morphMany('App\Vote', 'votable');
    }
public function votes()
{
    return $this->morphMany('App\Vote', 'votable');
}

public function post()
{
    return $this->belongsTo('App\Post','post_id','id'); 
}
 public function votable()
    {
        return $this->morphTo();
    }
评论模式:

 public function comments()
    {
        return $this->hasMany('App\Comment','post_id','id'); 
    }

   public function votes()
    {
        return $this->morphMany('App\Vote', 'votable');
    }
public function votes()
{
    return $this->morphMany('App\Vote', 'votable');
}

public function post()
{
    return $this->belongsTo('App\Post','post_id','id'); 
}
 public function votable()
    {
        return $this->morphTo();
    }
投票模式:

 public function comments()
    {
        return $this->hasMany('App\Comment','post_id','id'); 
    }

   public function votes()
    {
        return $this->morphMany('App\Vote', 'votable');
    }
public function votes()
{
    return $this->morphMany('App\Vote', 'votable');
}

public function post()
{
    return $this->belongsTo('App\Post','post_id','id'); 
}
 public function votable()
    {
        return $this->morphTo();
    }
我得到了所有的帖子和评论,但没有投票的帖子和评论

$posts=Post::all();
foreach($posts as $post)
{
     echo "<pre>"; print_r($post->post);    
     foreach($post->comments as $comment)
     {
        echo "<pre>"; print_r($comment->comment_body);
        foreach($comment->votes as $vote)
        {
            echo "<pre>"; print_r($vote->vote);
        }               
     }

    foreach($post->votes as $vote)
    {
        echo "<pre>"; print_r($vote->vote);
    }
}
$posts=Post::all();
foreach($posts作为$post)
{
回显“;打印($post->post);
Relation::morphMap([
  'Posts' => App\Post::class,
  'Comments' => App\Comment::class,
]);
foreach($post->comments as$comment) { echo”“;print_r($comment->comment_-body);
Relation::morphMap([
  'Posts' => App\Post::class,
  'Comments' => App\Comment::class,
]);
foreach($comment->投票为$vote) { echo”“;print_r($vote->vote);
Relation::morphMap([
  'Posts' => App\Post::class,
  'Comments' => App\Comment::class,
]);
} } foreach($post->投票为$vote) { echo”“;print_r($vote->vote);
Relation::morphMap([
  'Posts' => App\Post::class,
  'Comments' => App\Comment::class,
]);
} }

print\r($post->vots)
打印($comment->votes)不显示与投票的任何关系。

请使用App\CommentApp\Post而不是
votable
列中的Comment或Post

Laravel4.2过去只保留模型的名称,但Laravel5.2现在将它们存储在名称空间中

如果还有任何疑问,请告诉我:)

--编辑

正如@jaysingkar在评论中所问,我正在为这个问题添加执行morphMap的代码

使用以下命令创建您自己的服务提供商

然后,创建并将其放在名称空间之后

然后将其添加到
boot()
方法中


希望有帮助!:)

dd($post->voces)
dd($comment->voces)
给你什么?另外,你能发布投票表的截图吗?@prateekkathal我没有任何投票关系。你使用的是Laravel 5.2吗?因为你刚刚编辑了你的帖子并确认你使用的是Laravel 5.2,我相信
votable\u类型
应该是App\Comment或App\post,而不仅仅是Comment或post。请通过更改votable字段中的某些行进行确认,并让我知道您是否获得了预期的输出。:)@prateekkathal的条目是正确的。类型列需要模型类名。如果可能的话。你能不能也添加变形贴图实现?或者如果可以的话,我会编辑你的answer@jaysingkar完成!