Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌套查询结果的laravel分页_Laravel_Eloquent_Pagination_Paginator - Fatal编程技术网

嵌套查询结果的laravel分页

嵌套查询结果的laravel分页,laravel,eloquent,pagination,paginator,Laravel,Eloquent,Pagination,Paginator,我想创建一个查询来输出所有属于一个类别的文章,并对其进行分页。 我确实喜欢这样,但不管怎样,在输出上,我有一个没有分页器的简单的Illumb\Database\Eloquent\Collection帖子。 怎么做 在我的模型中,我有关系 一个类别有许多职位。 一篇文章属于一个类别 public function getItemWithPostsWithSimplePaginator($slug, $perPage = null) { $columns = ['id

我想创建一个查询来输出所有属于一个类别的文章,并对其进行分页。 我确实喜欢这样,但不管怎样,在输出上,我有一个没有分页器的简单的Illumb\Database\Eloquent\Collection帖子。 怎么做

在我的模型中,我有关系 一个类别有许多职位。 一篇文章属于一个类别

    public function getItemWithPostsWithSimplePaginator($slug, $perPage = null)
    {
        $columns = ['id', '_lft', '_rgt', 'parent_id', 'title', 'slug', 'icon', 'description', 'image', 'metatitle', 'metakey', 'metadesc'];

        $postcolumns = ['title','slug','category_id','image','fulltext','created_at'];

        $result = $this
            ->startConditions()
            ->whereSlug($slug)
            ->select($columns)
            ->with(['posts' => function($query) use(&$postcolumns, &$perPage) {
                $query->select($postcolumns)
                      ->wherePublished(true)
                      ->orderBy('created_at', 'ASC')
                      ->simplePaginate($perPage);
                }])
            ->first();

        return $result;
    }
如果我做了dd,我可以在输出集合中看到11篇文章,尽管
$perPage=10
,属于这个类别的文章总数是24篇。因此,它在一定程度上起了作用。 我唯一错过的就是paginator对象

如果我将
simplePaginate
替换为
paginate
,我可以看到包含10篇文章的集合,但无论如何,没有paginator的正常集合

App\Models\NewsCategory {#838 ▼
  +timestamps: false
  #guarded: []
  #connection: "mysql"
  #table: "news_categories"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:12 [▶]
  #original: array:12 [▶]
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [▼
    "posts" => Illuminate\Database\Eloquent\Collection {#905 ▼
      #items: array:11 [▼
        0 => App\Models\NewsPost {#925 ▶}
        1 => App\Models\NewsPost {#926 ▶}
        2 => App\Models\NewsPost {#927 ▶}
        3 => App\Models\NewsPost {#928 ▶}
        4 => App\Models\NewsPost {#929 ▶}
        5 => App\Models\NewsPost {#930 ▶}
        6 => App\Models\NewsPost {#931 ▶}
        7 => App\Models\NewsPost {#932 ▶}
        8 => App\Models\NewsPost {#933 ▶}
        9 => App\Models\NewsPost {#934 ▶}
        10 => App\Models\NewsPost {#935 ▶}
      ]
    }
  ]
  #touches: []
  #hidden: []
  #visible: []
  #fillable: []
  #pending: null
  #moved: false
}

您可以尝试的解决方案之一是使用setRelation()方法

$result = $result->setRelation(
    'posts', 
    YourPostModel::select($postcolumns)
        ->wherePublished(true)
        ->orderBy('created_at', 'ASC')
        ->simplePaginate($perPage)
);

你的意思是
setRelation()
而不是
with()
方法?我刚刚尝试了,这给了我错误
调用未定义的方法Kalnoy\Nestedset\QueryBuilder::setRelation()
我还尝试了用
注释
,并在
first()之后排队
setRelation()
。它只是给出
输出#可观测值:[]#关系:数组:1[▼     “posts”=>Closure($query){#803▼即使内部没有post项,似乎您正在使用kalnoy/nestedset包,而此包在其QueryBuilder上没有setRelation方法。这是不正确的。1)如果在我的模型中删除
使用kalnoy\nestedset\NodeTrait;
并尝试setRelation(),然后,如果我在first()之后用
和queue
setRelation()注释
,调用undefined方法illumb\Database\Eloquent\Builder::setRelation()
2),则会出现错误
调用。它只是给出输出
#可观测项:[]#关系:数组:1[▼ “posts”=>Closure($query){#788▼即使里面没有post项目。就像我使用kalnoy/nestedset包一样