Any easy way to assign paginates to a button?

Viewed 75

I create a custom <select> in order to paginate the results, but I don't know how to make usable.

I want to do the easiest way to work, and in my brain is link the paginate method in AlumnoController with this select options.

Can I? Or it's harder than I want to do?

EDIT: Thanks for your help @enrollment!! This is how code is right now. I think it's close to work and I like very much this code, can you help me to finish it?

web.php

Route::resource('alumnos', AlumnosController::class);
Route::get('/alumnos?{paginateValue}', [AlumnosController::class, 'showPaginated'])->name('alumnos');
Route::post('/alumnos', [AlumnosController::class, 'update'])->name('update.paginateValue');

AlumnosController.php

    public function index(Request $request)
    {
        $alumnos = Alumnos::select('ALU_ID', 'ALU_NOMBRE', 'ALU_APELLIDO1', 'ALU_APELLIDO2')
                    ->orderBy('ALU_ID', 'desc')
                    ->paginate(10);

        return view ('alumnos.alumnos', compact('alumnos'));
    }

index.blade.php

    <table class="datatable">
        <thead>
            <tr>
                <th>ID</th>
                <th>Nombre y apellidos</th>
            </tr>
        </thead>

        <tbody>
            @foreach ($alumnos as $alumno)
            <tr>
                <td>{{ $alumno->ALU_ID ?? '(Sin ID)' }}</td>
                <td>{{ $alumno->ALU_NOMBRE ?? '(Sin Nombre)' }} {{ $alumno->ALU_APELLIDO1 ?? '(Sin Apellido 1)' }} {{ $alumno->ALU_APELLIDO2 ?? '(Sin Apellido 2)' }}</td>
            </tr>
            @endforeach
        </tbody>
    </table>

    <div class="footer-tools">
        @isset($paginateValue)
        <div class="pages">
            {{$alumnos->links()}}
        </div>
        @endisset

        <form action="{{route('update.paginateValue')}}" method="POST">
            @csrf
            <select name="select" id="select" onchange="this.form.submit()">
                <option>Filter by quantity</option>
                <option value="5">5</option>
                <option value="10">10</option>
                <option value="15">15</option>
            </select>
        </form>
    </div>
3 Answers

After understanding your idea and trying to find a solution, I have achieved it in the following way:

When the user selects the number of students per page, the controller receives that number, and depending on whether it is shown in a different route, so as not to collide with the main "alumnos" route, I hope I have been able to help you.

AlumnoController

public function update(Request $request){
    $paginateValue = $request->select;
    switch ($paginateValue) {
        case '5':
            return redirect()->route('alumnos', [$paginateValue]);
        break;

        case '10':
            return redirect()->route('alumnos', [$paginateValue]);
        break;

        case '15':
            return redirect()->route('alumnos', [$paginateValue]);
        break;
        
        default:
            break;
    }
}

public function showPaginated($paginateValue){
    $alumnos = Alumnos::orderBy('NAME', 'asc')->paginate($paginateValue);
    return view('alumnos', compact('alumnos', 'paginateValue'));
}

public function index(){
    $alumnos = Alumnos::orderBy('NAME', 'asc')->paginate(15);
    return view('alumnos', compact('alumnos'));
}

index.blade.php

  @foreach ($alumnos as $alumno)
        {{$alumno->name}}
  @endforeach

  {{$alumnos->links()}}
           
  <form action="{{route('update.paginateValue')}}" method="POST">
      @csrf
      <select name="select" id="select" onchange="this.form.submit()">
          <option>Filter by quantity</option>
          <option value="5">5</option>
          <option value="10">10</option>
          <option value="15">15</option>
      </select>
  </form>

web.php

Route::get('/alumnos', [AlumnoController::class, 'index']);
Route::get('/alumnos&{paginateValue}', [AlumnoController::class, 'showPaginated'])->name('alumnos');
Route::post('/alumnos', [AlumnoController::class, 'update'])->name('update.paginateValue');;
Related