laravel 5.2 pagination pretty url

2019-06-05 11:41发布

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条回答
相关推荐>>
2楼-- · 2019-06-05 12:19

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() }}
查看更多
不美不萌又怎样
3楼-- · 2019-06-05 12:31

So you can try something like that:

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

查看更多
登录 后发表回答