devamit2018's avatar

pagination by drop down

i want to paginate the view page according to drop-down values selected....

0 likes
19 replies
burlresearch's avatar

Customizing The Pagination View

By default, the views rendered to display the pagination links are compatible with the Bootstrap CSS framework. However, if you are not using Bootstrap, you are free to define your own views to render these links. When calling the links method on a paginator instance, pass the view name as the first argument to the method:

{{ $paginator->links('view.name') }}

// Passing data to the view...
{{ $paginator->links('view.name', ['foo' => 'bar']) }}

However, the easiest way to customize the pagination views is by exporting them to your resources/views/vendor directory using the vendor:publish command:

php artisan vendor:publish --tag=laravel-pagination

This command will place the views in the resources/views/vendor/pagination directory. The default.blade.php file within this directory corresponds to the default pagination view. Simply edit this file to modify the pagination HTML.

devamit2018's avatar

this is my view page 5 10

this is my controller

public function getreport() {

$school = DB::table('school')

    ->Paginate(10);
    
        $links = str_replace('/?', '?', $school->render());
        
        return view('schoolview',compact('school'));

    }

i want to pass selected option to paginate value

deansatch's avatar

Just make your drop down post to getReport route and pass it through as a request value

Paginate($request->number)

devamit2018's avatar

i did it without passing through route it works but the problem is after restarting it throws null value... mine view.blade.php is

<select name ="number">
  <option value="5">5</option>
  
  <option value="25">25</option>
  </select>
     <input type="hidden" name ="_token" value="{{ csrf_token() }}">
     <button type="submit" class="btn btn-primary">GO</button>
       </form>

and mine controller is public function getreport(Request $request) {

        $value =$request->get('number');
    
        $school = DB::table('school')
        
        ->Paginate($value);
    
        $links = str_replace('/?', '?', $school->render());
        
        return view('daily_entry_report',compact('school'));

    }
Snapey's avatar
Snapey
Best Answer
Level 122

here's a solution. In this case, I am listing members of a club.

    public function index(Request $request)
    {
        $items = $request->items ?? 10;      // get the pagination number or a default

        $club = Club::findOrFail(session('club'));

        $members = $club->members()->paginate($items);
       
        return view('club.' . config('app.locale') . '.index')
            ->withClub($club)
            ->withMembers($members)
            ->withItems($items);
    }

The view;

    <form>
        <select id="pagination">
            <option value="5" @if($items == 5) selected @endif >5</option>
            <option value="10" @if($items == 10) selected @endif >10</option>
            <option value="25" @if($items == 25) selected @endif >25</option>
        </select>
    </form>

    <script>
        document.getElementById('pagination').onchange = function() {
            window.location = "{{ $members->url(1) }}&items=" + this.value;
        };
    </script>

So, the dropdown called pagination has the page length choices. It looks at the $items variable to see which option should be selected

The javascript reloads the page automatically if the quantity changes. It always loads page 1 but this could be refactored to load the correct page given the page they are on and the new length required.

The number chosen is passed back to the controller as items and used to set the new page length

2 likes
petar_vukcevic's avatar

@Snapey is '->withMembers($members)' a method you declared in a model class? if it is can we see it?

devamit2018's avatar

thanks snapey....yours only $items = $request->items ?? 10; did my work....no need to add.. since i had not set default value so after restart it was giving null value...i guess it would help many other learners who is beginner in laravel

devamit2018's avatar

what is url1 in this case?is it routers link or else

Snapey's avatar

->url(1) is a function against the paginator. it returns the full url of the current page with ?page=1 added to the end

This means I can use & to add the items variable without worrying if the current url does or does not contain the ?page= parameter

devamit2018's avatar

Call to a member function url() on null it returns error after passing it should i give current page address like htttp:school/.... instead of url(1) or what...i have done it by other

process

Snapey's avatar

You need to use ->url() on a paginator. You have not shown any of your code so I don't know if you are doing that

devamit2018's avatar

public function paginate(Request $request) {

$items = $request->items ?? 10; // get the pagination number or a default

    $club =DB::table('students')->findOrFail(session('club'));

    $members = $club->members()->paginate($items);
   
    return view('club.' . config('app.locale') . '.index')
        ->withClub($club)
        ->withMembers($members)
        ->withItems($items);

}

my controller

devamit2018's avatar

my route

Route::get('/', function () { return view('welcome'); });

Route::get('/getAdd', function () {

});

Route::get('/result','AdminStudentsController@getresult'); Route::get('/paginate','AdminStudentsController@paginate');

devamit2018's avatar
1 2
    </select>
</form>

<script>
    document.getElementById('pagination').onchange = function() {
        window.location = "{{ $members->url(1) }}&items=" + this.value;
    };
</script>

my blade.php

Snapey's avatar

You cannot just literally copy my code ! You need to make it work for you.

rakesh2018's avatar

what is club and members in this case?is club database table ?

niketbjoshi's avatar

@Snapey pagination is not work as you suggest i'll do excellently same but when i move to 32 or 3 page it's not working can you please help me @Snapey

niketbjoshi's avatar

@Snapey Thank you for guiding me in correct path i'll do that as per i needed and it's done correctyl

Please or to participate in this conversation.