Laravel - Union + Paginate at the same time?

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

    I had this same problem, and unfortunately I couldn't get the page links with {{ $result->links() }}, but I found another way to write the pagination part and the page links appears

    Custom data pagination with Laravel 5

    //Create a new Laravel collection from the array data
    $collection = new Collection($searchResults);
    
    //Define how many items we want to be visible in each page
    $perPage = 5;
    
    //Slice the collection to get the items to display in current page
    $currentPageSearchResults = $collection->slice($currentPage * $perPage, $perPage)->all();
    
    //Create our paginator and pass it to the view
    $paginatedSearchResults= new LengthAwarePaginator($currentPageSearchResults, count($collection), $perPage);
    
    return view('search', ['results' => $paginatedSearchResults]);
    

提交回复
热议问题