May Sale! All accounts are 40% off this week.

stefancoding's avatar

Page pagination with numbers

Hi all, I try to get page pagination with numbers but all what showing me is only Next and Previews button.

$products = Product::whereIn('id', $productId)
                ->where('status', '=', 'Live')
                ->orderBy('quantity', 'desc') 
                ->paginate(10);

in my blade file I added this code

{{ $products->links() }}
0 likes
8 replies
Braunson's avatar

Docs are key here. https://laravel.com/docs/9.x/pagination#adjusting-the-pagination-link-window

When the paginator displays pagination links, the current page number is displayed as well as links for the three pages before and after the current page. Using the onEachSide method, you may control how many additional links are displayed on each side of the current page within the middle, sliding window of links generated by the paginator:

{{ $users->onEachSide(5)->links() }}
stefancoding's avatar

@Braunson Thank you but when I using onEachSide still displaying only the Next and Previes buttons and not the Number for the pagination.

ehab.aboshehab's avatar

@stefancoding

Try something like this :

$page = (int) ($request->page ?? 1);
$per_page = (int) ($request->per_page ?? 10);
$products = Product::whereIn('id', $productId)
->where('status', '=', 'Live')
->orderBy('quantity', 'desc');

$total_items = $products->count();
$products = $products->paginate($per_page);
$total_pages = $products->lastPage();

Then you can know how many pages do you have ....

AshboDev's avatar

What version of Laravel are you using? With the default pagination stuff in latest versions, this is something which works out the box: https://laravel.com/docs/9.x/pagination#paginating-eloquent-results

This is what I use for one of my main projects, Laravel 8 but the concept is the same, this is how it displays frontend:

https://imgur.com/tsn0FZf

And the code is:

$events = Event::orderBy('event_start', 'desc')->paginate(20);

        return view('events.history', compact('events'));

HTH, Ash

stefancoding's avatar

Thank you guy but I found the solution. The problem is was that in Providers/AppServiceProvider.php was not added this line

use Illuminate\Pagination\Paginator;
public function boot()
    {
        Paginator::useBootstrap();
    }
abolfazlahmadiweb's avatar

laravel use twindcss so fix this problem write this appserviceprovider

public function boot()
    {
        Paginator::useBootstrap();
    }
abolfazlahmadiweb's avatar

@Snapey Unfortunately, you are quick to judge that due to the prohibition of Iranian programmers by Laracast, sometimes the messages are not displayed and ... or it happens with a delay.

Please or to participate in this conversation.