1

I am Using Laravel 5.2

Is There a Way To Get a Pagination Pretty URL in Laravel 5.2?

http://localhost:8000/backend/admin_user?page=10&page=1

And What I Would Like To Get,How generate Link Pretty Url:

http://localhost:8000/backend/admin_user/10/1

2 Answers2

1

So you can try something like that:

Route::get('test/{page}', function ($page) { return User::paginate(2, ['*'], 'page', $page); });

Vuer
  • 149
  • 6
  • How Generate Links Pagination url=backend/admin_user/10/1 – Omid Salmani Aug 24 '16 at 09:21
  • You cannot put two parameters named page. If you have two pagination you need to put two params with different names: Route::get('test/{page}/{page1}', function ($page, $page1) { $users = User::paginate(2, ['*'], 'page', $page); $posts = Post::paginate(2, ['*'], 'page1', $page1); }); – Vuer Aug 24 '16 at 11:08
  • @Vuer - Where I need to write the following code: Route::get('test/{page}', function ($page) { return User::paginate(2, ['*'], 'page', $page); }); – Niladri Banerjee - Uttarpara Feb 11 '17 at 13:16
0

You can achieve this with three simple steps.

Register the route:

Note the question mark, this makes the size and page values optional;

Route::get('backend/admin_user/{size?}/{page?}', ['uses' => 'BackendController@adminUser']);

Implement this function in your controller:

Note the default values, $size = 10, $page = 1. This makes sure that you don't get an error if you navigate to the url without the pagination.

<?php namespace App\Http\Controllers;

use App\Models\AdminUser;
use Illuminate\Pagination\LengthAwarePaginator;

class BackendController
{
    public function adminUser($size = 10, $page = 1)
    {
        $collection = AdminUser::all();
        $users = new LengthAwarePaginator($collection, $collection->count(), $size);
        $users->resolveCurrentPage($page);

        return view(backend.admin_user);
    }
}

Use in your view like this:

<div class="container">
    @foreach ($users as $user)
        {{ $user->name }}
    @endforeach
</div>

{{ $users->links() }}
akalucas
  • 476
  • 3
  • 13