我如何加入两个查询然后分页
How do i join two queries and then paginate
$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() 方法,例如:
$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);
应该可以。
在控制器中加入查询和分页时,要在分页中查看输出,您应该在循环输出中包含以下内容:
{{ $data->links() }}
$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() 方法,例如:
$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);
应该可以。
在控制器中加入查询和分页时,要在分页中查看输出,您应该在循环输出中包含以下内容:
{{ $data->links() }}