gpt4 book ai didi

php - Laravel - 同时联合+分页?

转载 作者:IT王子 更新时间:2023-10-29 00:12:39 25 4
gpt4 key购买 nike

简介:

我正在尝试合并 2 个表 recipesposts,然后将 ->paginate(5) 添加到查询中。

但出于某种原因我得到了这个错误:

Cardinality violation: 1222 The used SELECT statements have a different number of columns (SQL: (select count(*) as aggregate from posts

代码:

$recipes = DB::table("recipes")->select("id", "title", "user_id", "description", "created_at")
->where("user_id", "=", $id);

$items = DB::table("posts")->select("id", "title", "user_id", "content", "created_at")
->where("user_id", "=", $id)
->union($recipes)
->paginate(5)->get();

我做错了什么吗?

没有 ->paginate(5) 查询工作正常。

最佳答案

你是对的,分页会导致问题。现在,您可以创建一个 View 并查询该 View 而不是实际的表,手动创建您的Paginator:

$page = Input::get('page', 1);
$paginate = 5;

$recipes = DB::table("recipes")->select("id", "title", "user_id", "description", "created_at")
->where("user_id", "=", $id);
$items = DB::table("posts")->select("id", "title", "user_id", "content", "created_at")
->where("user_id", "=", $id)
->union($recipes)
->get();

$slice = array_slice($items->toArray(), $paginate * ($page - 1), $paginate);
$result = Paginator::make($slice, count($items), $paginate);

return View::make('yourView',compact('result'));

关于php - Laravel - 同时联合+分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25338456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com