Laravel - Union + Paginate at the same time?

后端 未结 12 594
有刺的猬
有刺的猬 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

    order by

     $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)
                ->orderBy('created_at','desc')
                ->get();
    
    $slice = array_slice($items, $paginate * ($page - 1), $paginate);
    $result = Paginator::make($slice, count($items), $paginate);
    
    return View::make('yourView',compact('result'))->with( 'result', $result );
    

    View page :

       @foreach($result as $data)
      {{ $data->your_column_name;}}
     @endforeach 
    
      {{$result->links();}}   //for pagination
    

    its help to more peoples.. because nobody cant understand show data in view page union with pagination and orderby .. thank u

提交回复
热议问题