In this tutorial, we will see how to add laravel inertia pagination with vue 3 using breeze. Recently laravel breeze come with vue js 3.
Laravel 8 Inertia Js Pagination with vue 3
Step 1: Install Laravel & Connect Database
Step 2: Install Breeze & Setup Inertia Js Vue 3
Step 3: Create Model Factory and Controller & route
Step 4: Create a Vue js Pagination Component
Step 5: Show Post List with Pagination
Step 1: Install Laravel & Connect Database
Create a new laravel app.
composer create-project --prefer-dist laravel/laravel inertia-pagination
Now, you have to connect the laravel app to the database, hence open the .env configuration file and add the database credentials as suggested below.
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_user_name
DB_PASSWORD=database_password
Step 2: Install Breeze & Setup Inertia Js Vue 3
Install laravel breeze via composer
composer require laravel/breeze --dev
Next, run below command
php artisan breeze:install
install breeze with vue 3
php artisan breeze:install vue
And final install Dependencies
npm install && npm run dev
Step 3: Create Model Factory and Controller & route
First we need to create Post model.
php artisan make:model Post -mf
Model created successfully.
Factory created successfully.
Created Migration: 2021_09_14_112455_create_posts_table
Controller created successfully.
Post Table
app/database/migrations/posts_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('description');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Post Model
app/Models/Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'title',
'description'
];
use HasFactory;
}
Post Factory
app/database/factories/PostFactory.php
<?php
namespace Database\Factories;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
class PostFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Post::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'title' => $this->faker->title(),
'description' => $this->faker->text(),
];
}
}
After set up factory we need to create fake data, So we will use laravel tinker
Create fake data using tinker
php artisan ti
Psy Shell v0.10.8 (PHP 7.4.23 — cli) by Justin Hileman >>> App\Models\Post::factory(30)->create();
Post Controller
app/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
use Inertia\Inertia;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::latest()->paginate(10);
return Inertia::render(
'Post/Index',
['posts' => $posts]
);
}
}
app/routes/web.php
<?php
use App\Http\Controllers\PostController;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return Inertia::render('Welcome', [
'canLogin' => Route::has('login'),
'canRegister' => Route::has('register'),
'laravelVersion' => Application::VERSION,
'phpVersion' => PHP_VERSION,
]);
});
Route::get('/dashboard', function () {
return Inertia::render('Dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');
Route::get('posts', [PostController::class,'index'])->name('posts.index');
require __DIR__.'/auth.php';
Step 4: Create a Vue js Pagination Component
You need to create pagination component, if you are using vue js 3 then copy first below code, for vue js 2 copy second component code.
app/resources/Components/Pagination.vue
Vue Js 3
<template>
<div v-if="links.length > 3">
<div class="flex flex-wrap -mb-1">
<template v-for="(link, key) in links">
<div
v-if="link.url === null"
:key="key"
class="
px-4
py-3
mb-1
mr-1
text-sm
leading-4
text-gray-400
border
rounded
"
v-html="link.label"
/>
<Link
v-else
class="
px-4
py-3
mb-1
mr-1
text-sm
leading-4
border
rounded
hover:bg-white
focus:border-indigo-500 focus:text-indigo-500
"
:class="{ 'bg-white': link.active }"
:href="link.url"
v-html="link.label"
/>
</template>
</div>
</div>
</template>
<script>
import { Link } from "@inertiajs/inertia-vue3";
export default {
components: {
Link,
},
props: {
links: Array,
},
};
</script>
Vue Js 2
<template>
<div v-if="links.length > 3">
<div class="flex flex-wrap -mb-1">
<template v-for="(link, key) in links">
<div v-if="link.url === null" :key="key" class="px-4 py-3 mb-1 mr-1 text-sm leading-4 text-gray-400 border rounded" v-html="link.label" />
<inertia-link v-else :key="key" class="px-4 py-3 mb-1 mr-1 text-sm leading-4 border rounded hover:bg-white focus:border-indigo-500 focus:text-indigo-500" :class="{ 'bg-white': link.active }" :href="link.url" v-html="link.label" />
</template>
</div>
</div>
</template>
<script>
export default {
props: {
links: Array,
},
}
</script>
Step 5: Show Post List with Pagination
app/resources/js/Pages/Post/Index.vue
<template>
<Head title="Dashboard" />
<BreezeAuthenticatedLayout>
<template #header>
<h2 class="text-xl font-semibold leading-tight text-gray-800">
Post Index
</h2>
</template>
<div class="py-12">
<div class="container mx-auto sm:px-6 lg:px-8">
<div
class="
overflow-hidden
bg-white
shadow-sm
max-w-7xl
sm:rounded-lg
"
>
<div class="p-6 bg-white border-b border-gray-200">
<table>
<thead class="font-bold bg-gray-300 border-b-2">
<td class="px-4 py-2">ID</td>
<td class="px-4 py-2">Title</td>
<td class="px-4 py-2">Description</td>
<td class="px-4 py-2">Action</td>
</thead>
<tbody>
<tr v-for="post in posts.data" :key="post.id">
<td class="px-4 py-2">{{ post.id }}</td>
<td class="px-4 py-2">{{ post.title }}</td>
<td class="px-4 py-2">
{{ post.description }}
</td>
<td class="px-4 py-2">Action</td>
</tr>
</tbody>
</table>
<pagination class="mt-6" :links="posts.links" />
</div>
</div>
</div>
</div>
</BreezeAuthenticatedLayout>
</template>
<script>
import BreezeAuthenticatedLayout from "@/Layouts/Authenticated.vue";
import { Head } from "@inertiajs/inertia-vue3";
import Pagination from "@/Components/Pagination.vue";
export default {
components: {
BreezeAuthenticatedLayout,
Head,
Pagination,
},
props: {
posts: Object,
},
};
</script>
Note: Always try to open to terminal window one use for server and second for below command.
npm run dev