1

I have merged a collection and now I want to be able to paginate it. I've read this stackoverflow answer on forPage but I don't quite understand how to apply it. Can someone please help clarify? Thanks!

In my Controller:

$lofilter = request('locationfilter');

$mypostings = Postings::where('location', 'LIKE', '%'. $lofilter .'%')->get();
$otherpostings = Postings::where('location', 'NOT LIKE', '%'. $lofilter .'%')->get();

$postings = $mypostings->merge($otherpostings)->paginate(20);
linktoahref
  • 7,812
  • 3
  • 29
  • 51
jlim
  • 91
  • 4
  • 11

1 Answers1

0

The answer lies in the link that you've shared. In your code $postings is a collection and collection does not have paginate() method. Instead use the forPage() method as stated in the link

$postings = $mypostings->merge($otherpostings);
$links = $postings->forPage(1, 20);

The first parameter is the page number and the second parameter is the number of items you'd want to display per page. The documentation was pretty self-explanatory though!

UDPATE:

You need to paginate it manually, you could do this in your view (Pseudo Code)

{{ $totalPages = count($postings) }}

@foreach ($links as $link)
/* Display the links */
@endforeach

/* Create Pagination Links */
@for ($i = 1; $i <= $totalPages; $i++)
Create the links
@endfor
linktoahref
  • 7,812
  • 3
  • 29
  • 51
  • Is there an equivalent of links() that I can use in the view when I use forPage? – jlim Nov 27 '17 at 05:28
  • `$links` is just a variable, use whatever name that is relevant to your application – linktoahref Nov 27 '17 at 05:28
  • Oh sorry I meant: I am trying to do the equivalent of {{ $postings->links() }} in my view to show the page number and page links, but since I am not using paginate function it says the "links method does not exist". How do I display this in the view? – jlim Nov 27 '17 at 05:33
  • Please check the update in answer, though not tested – linktoahref Nov 27 '17 at 05:39