【问题标题】:How do i join two queries and then paginate我如何加入两个查询然后分页
【发布时间】:2018-11-08 13:49:32
【问题描述】:
    $data = Category::join('articles', function ($join) {
       $join->on('categories.id', 'articles.category_id')
        ->where('categories.type', 'news')
        ->where('articles.status', '1');
    })->get()->sortByDesc('updated_at')->paginate(5);

我有 2 个变量都在查询同一个模型。我现在想将这两个单独的数据集合并到一个名为 $articles 的变量中,然后对其进行分页。

【问题讨论】:

  • 只需删除 get() ,您的查询就可以工作了。

标签: laravel-5 eloquent laravel-paginate


【解决方案1】:

从代码中删除 get() 方法,如:

$data = Category::join('articles', function ($join) {
    $join->on('categories.id', 'articles.category_id')
    ->where('categories.type', 'news')
    ->where('articles.status', '1');
})->sortByDesc('updated_at')->paginate(5);

它应该可以工作。

【讨论】:

    【解决方案2】:

    当在控制器中加入查询和分页时,要在分页中查看输出,您应该在循环输出下包含以下内容:

    {{ $data->links() }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多