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

2 Answers 2

1

So you can try something like that:

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

Sign up to request clarification or add additional context in comments.

3 Comments

How Generate Links Pagination url=backend/admin_user/10/1
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 - Where I need to write the following code: Route::get('test/{page}', function ($page) { return User::paginate(2, ['*'], 'page', $page); });
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() }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.