PHP code example of yonkosam / laravel-cached-pagination

1. Go to this page and download the library: Download yonkosam/laravel-cached-pagination library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yonkosam / laravel-cached-pagination example snippets


// Before: Regular pagination (hits database every time)
$users = User::where('active', true)->paginate(15);

// After: Cached pagination (cached for 1 hour by default)
$users = User::where('active', true)->cachedPaginate();

return [
    /**
     * The default Time To Live (TTL) for cached paginations in seconds.
     * You can use an integer for seconds or a \DateInterval object.
     * The default is 1 hour (3600 seconds).
     */
    'ttl' => 3600,

    /**
     * Determine whether the pagination cache should be automatically
     * cleared when a model is updated.
     */
    'clear_on_update' => true,

    /**
     * Determine whether the pagination cache should be automatically
     * cleared when a model is created.
     */
    'clear_on_create' => true,

    /**
     * Determine whether the pagination cache should be automatically
     * cleared when a model is deleted.
     */
    'clear_on_delete' => true,
];



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Yonko\LaravelCachedPagination\Traits\HasCachedPagination;

class User extends Model
{
    use HasCachedPagination;

    // Your model code...
}



namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
        // Cache pagination results for 1 hour (default)
        $users = User::where('active', true)
            ->cachedPaginate(15);

        return view('users.index', compact('users'));
    }
}

// Standard pagination (hits database every time)
$posts = Post::published()->paginate(10);

// Cached pagination (cached for 1 hour by default)
$posts = Post::published()->cachedPaginate(10);

// Cache for 30 minutes (1800 seconds)
$posts = Post::published()->cachedPaginate(ttl: 1800, perPage: 10);

// Cache for 24 hours
$posts = Post::published()->cachedPaginate(ttl: 86400, perPage: 10);

$posts = Post::published()->cachedSimplePaginate(10);

// With custom TTL
$posts = Post::published()->cachedSimplePaginate(ttl: 3600, perPage: 10);

$posts = Post::published()->cachedCursorPaginate(10);

// With custom TTL
$posts = Post::published()->cachedCursorPaginate(ttl: 3600, perPage: 10);

$users = User::with(['posts', 'profile'])
    ->where('status', 'active')
    ->where('created_at', '>=', now()->subMonths(6))
    ->orderBy('last_login_at', 'desc')
    ->cachedPaginate(20);

// Clear all cached pagination for User model
User::clearCachedPaginators();

// This will automatically happen when a User is created, updated, or deleted
// (based on your configuration)

// config/cached-pagination.php
return [
    'clear_on_create' => true,  // Clear cache when new models are created
    'clear_on_update' => true,  // Clear cache when models are updated
    'clear_on_delete' => true,  // Clear cache when models are deleted
];

use App\Http\Resources\PostResource;

class PostController extends Controller
{
    public function index()
    {
        $posts = Post::published()->cachedPaginate(15);

        return PostResource::collection($posts);
    }
}

DB::enableQueryLog();
$posts = Post::cachedPaginate(10);
dd(DB::getQueryLog()); // Should be empty on cache hit
bash
php artisan vendor:publish --tag="laravel-cached-pagination-config"
blade
<!-- resources/views/posts/index.blade.php -->
@foreach($posts as $post)
    <article>
        <h2>{{ $post->title }}</h2>
        <p>{{ $post->excerpt }}</p>
    </article>
@endforeach

{{ $posts->links() }}
bash
composer analyse