Pagination

Making pagination with laravel inertia

I wanna show how to make laravel pagination with inertia. In this topic I don't refer step by step guide but show you some controller and inertia files. Hope you can understand. Let's start it.

UsersController.php

class UsersController extends Controller
{
    public function index()
    {
        return Inertia::render('Users', [
            'users' => User::paginate(10)->through(fn($user) => [
                'id' => $user->id,
                'name' => $user->name
            ])
        ]);
    } 
}

The laravel collection of through and map methods are same approach.

https://laravel.com/docs/8.x/collections#method-map

Pagination.vue

<template>
  <div>
    <Component
      :is="link.url ? 'Link' : 'span'"
      v-for="link in links"
      :href="link.url"
      v-html="link.label"
      class="px-1"
      :class="{ 'text-gray-500': ! link.url, 'font-bold' : link.active }"
    />
  </div>
</template>

<script>
export default {
  props: {
    links: Array
  }
};
</script>

Users.vue

<template>
  //
  <Pagination :links="users.links" />
</template>

<script setup>
import Pagination from "../Shared/Pagination";
defineProps({ users: Object });
</script>

Hope you got it.

Last updated