Laravel - Union + Paginate at the same time?

后端 未结 12 563
有刺的猬
有刺的猬 2020-12-09 04:20

Brief:

I am trying to union 2 tables recipes and posts then add ->paginate(5) to the queries.

But

12条回答
  •  粉色の甜心
    2020-12-09 04:34

    $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()->toArray();
    $slice = array_slice($items, $paginate * ($page - 1), $paginate);
    $result = new Paginator($slice , $paginate);
    

提交回复
热议问题