如何在laravel中为嵌套的注解添加分页?

vkc1a9a2  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(86)
class Nested extends Model{
  public function nestedfunc()
{
    return $this->hasMany(Nested::class, 'parent_id');
}
 public function childnestedfunc()
{
    return $this->hasMany(Nested::class, 'parent_id')->with('nestedfunc');
}
}

字符串
//这是我的递归模型函数。如何将分页添加到嵌套注解的根。我将所有注解存储到同一个表中
1.根注解1 -根1的嵌套注解....
1.根注解2 -根2的嵌套注解....
1.给根注解标页码
1.根注解3 -根3的嵌套注解....

  1. root comments 4 -nested commets of root 4.
    1.给根注解标页码
    1.等
cx6n0qe3

cx6n0qe31#

这里不需要两个独立的函数,关系可以递归地调用自己,所以首先将关系更改为

public function children()
{
    return $this->hasMany(Nested::class, 'parent_id')->with('children');
}

字符串
那么对于你的查询,你可以只做

Nested::where('parent_id', null)->with('children')->paginate();


在构建嵌套注解模型时,我还添加了一个作用域,使其更具可读性

public function scopeTopLevel($query)
{
   return $query->where('parent_id', null);
}


所以现在我的控制器里的调用

Comment::topLevel()->with('children')->paginate()

相关问题