xaad67's avatar

Laravel How to create A-Z pagination

In laravel How can I create A-Z pagination for example I've a table users

I want to create sorting pagination from there first letter like In character A all the users starting with A must be there same with A-Z

0 likes
1 reply
diegoaurino's avatar

Hello, @xaad67 !

You need to create something like the following:

  • order your collection by title and group them by the first letter:

Company::orderBy('brand')->get()->groupBy(function($company) ({
    # insert your restriction 
}));

  • inside the groupBy method pass a closure with your restriction, for example, the first letter. You can use substr to get the first character of a string.

https://www.php.net/manual/en/function.substr.php

  • in your views, filter these collections as needed. 

Check the docs for how to group these collections: https://laravel.com/docs/5.8/queries 

Let me know if it helps.

Please or to participate in this conversation.