Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

115
Vistas
How to fetch data using AJAX LARAVEL with pagination

can you suggest a way on how can I fetch a data using AJAX LARAVEL with pagination? I already have a fetch function using AJAX and it is already working, but I wanted to add a pagination. Thank you in advance.

This is my controller

public function fetchAllCustomer()
{

    $customers = Customer::all();
    return response()->json([
        'customers'=>$customers,
    ]);
}

This is my AJAX function

function fetchstudent() {
      $.ajax({
          type: "GET",
          url: "/fetch-customer",
          dataType: "json",
          success: function (response) {
              // console.log(response);
              $('tbody').html("");
              $.each(response.customers, function (key, item) {
                  $('tbody').append('<tr>\
                      <td>' + item.first_name + '</td>\
                      <td><button type="button" value="' + item.id + '" class="btn btn-primary editbtn btn-sm">Edit</button></td>\
                      <td><button type="button" value="' + item.id + '" class="btn btn-danger deletebtn btn-sm">Delete</button></td>\
                  \</tr>');
              });
          }
      });
  }
9 months ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Laravel Docs - Pagination using Eloquent: https://laravel.com/docs/9.x/pagination#paginating-eloquent-results

Instead of:

public function fetchAllCustomer()
{

$customers = Customer::all();
return response()->json([
    'customers'=>$customers,
]);
}

Should be:

    public function fetchAllCustomer()
{
    $customers = Customer::paginate(15);
    // handle your pagination links in AJAX
    $paginationLinks = (string) $customers->links();
    return response()->json([
        'customers'  =>$customers,
        'pagination' => $paginationLinks
    ]);
}
9 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.