This guide shows how to use this package from a downloaded local copy before it is published to Packagist.
Assume the package is downloaded here:
/Users/ghostcompiler/Desktop/laravel-model-cachingIn your Laravel application's composer.json, add a local path repository:
{
"repositories": [
{
"type": "path",
"url": "../laravel-model-caching",
"options": {
"symlink": true
}
}
]
}Use the correct relative path from your Laravel app to this package. For example, if both projects are on the Desktop:
"url": "../laravel-model-caching"Then require the package inside your Laravel app:
composer require ghostcompiler/laravel-model-caching:@devFor Laravel 13 apps, make sure this package version includes Laravel 13 constraints:
"illuminate/cache": "^10.0|^11.0|^12.0|^13.0"If Composer already has a cached package resolution, update it:
composer update ghostcompiler/laravel-model-cachingIf the package was previously rejected by Composer, run this from the Laravel app after updating the downloaded package:
composer clear-cache
composer require ghostcompiler/laravel-model-caching:@dev -WThe -W flag allows Composer to resolve related dependencies when needed.
php artisan vendor:publish --tag=model-cache-configThis creates:
config/model-cache.phpAdd the trait only to models that should support caching:
use GhostCompiler\LaravelModelCaching\Concerns\HasModelCaching;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasModelCaching;
}Then use:
User::with('posts.comments')->remember()->get();
User::with('posts')->remember(300)->paginate(10);
User::where('active', true)->remember()->firstOrFail();
User::findCached(1);remember() with no argument uses default_ttl from config/model-cache.php.
The trait does not cache by itself. Cached builder methods are get, first, firstOrFail, find, findOrFail, and pagination helpers.
To cache every read on trait-enabled models without chaining remember():
'auto_remember' => true,Use dontCache() to skip caching for a specific chain.
For development:
'store' => null,
'auto_remember' => false,
'cache_tags' => false,
'debug' => true,For tenant middleware that resolves on every request:
'auto_remember' => true,
'debug' => true,
'context_callbacks' => [
'host' => fn () => request()->getHost(),
],For production with Redis:
'store' => 'redis',
'cache_tags' => true,
'use_redis_sets' => true,For multi-tenant apps, include tenant context:
'context_callbacks' => [
'tenant' => fn () => tenant('id'),
],From this package directory:
composer install
composer validate --strict
composer analyse
composer testRun one syntax check manually:
find src config tests -name '*.php' -print0 | xargs -0 -n1 php -lAfter path installing, use the package exactly like a normal dependency:
php artisan config:clear
php artisan cache:clear
php artisan model-cache:warm "App\Models\User" --with=posts --ttl=600
php artisan model-cache:inspect "App\Models\Post" 5
php artisan model-cache:flush "App\Models\Post" 5If Laravel does not auto-discover the provider, add it manually in config/app.php:
'providers' => [
GhostCompiler\LaravelModelCaching\ModelCacheServiceProvider::class,
],This repository includes a test workflow at:
.github/workflows/tests.ymlIt runs:
- Composer validation
- PHP syntax checks
- PHPUnit on Laravel 10, 11, 12, and 13
The workflow runs on pushes and pull requests to main and master.
When the package is ready to publish:
- Push this repository to GitHub.
- Create a tagged release, for example
v1.0.0. - Submit the repository to Packagist.
- In Laravel apps, replace the path repository install with:
composer require ghostcompiler/laravel-model-caching