现有两个模型文章(Post)评论(Comment),模型定义如下
Post
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use YuanChao\Editor\EndaEditor;
class Post extends Model
{
protected $fillable = ['user_id', 'title', 'content'];
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->morphMany(Comment::class, 'commentable')->orderBy('created_at', 'desc');
}
}
Comment
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use YuanChao\Editor\EndaEditor;
class Comment extends Model
{
protected $fillable = ['user_id', 'commentable_id', 'commentable_type', 'body'];
/**
* Get all of the owning commentable models.
*/
public function commentable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo(User::class);
}
/**
* 子集评论
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function comments()
{
return $this->morphMany(self::class, 'commentable')->orderBy('created_at', 'desc');
}
}
如上所示一篇文章可以有多个评论,每个评论又可以有多个子评论,我遇到的问题是在模板渲染时可以通过“渴求势加载”方式在模板页嵌套循环输出评论。但是我现在要使用VUE把评论做成一个模块,需要请求得到评论的tree型结构,这个有什么好的处理方式吗