Brief:
I am trying to union 2 tables recipes
and posts
then add ->paginate(5)
to the queries.
But for some reason I get this error:
Cardinality violation: 1222 The used SELECT statements have a different number of columns (SQL: (select count(*) as aggregate from
posts
Code:
$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();
Am i doing something wrong?
Without ->paginate(5)
the query works fine.
You're right, pagination cause problem. Right now, you can create a view and query the view instead of the actual tables, or create your Paginator
manually:
$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'));