PHP code example of mikebronner / laravel-model-caching
1. Go to this page and download the library: Download mikebronner/laravel-model-caching 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/ */
mikebronner / laravel-model-caching example snippets
$posts = Cache::remember('posts:active:page:1', 3600, function () {
return Post::where('active', true)->with('comments')->paginate();
});
// And in every observer or event listener…
Cache::forget('posts:active:page:1');
// Hope you remembered every key variant! 😅
// Just query. Caching and invalidation happen automatically. ✨
$posts = Post::where('active', true)->with('comments')->paginate();
namespace App\Models;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Model;
abstract class BaseModel extends Model
{
use Cachable;
}
namespace App\Models;
use GeneaLabs\LaravelModelCaching\CachedModel;
class Post extends CachedModel
{
// ...
}
class Post extends BaseModel
{
public function comments()
{
return $this->hasMany(Comment::class);
}
public function tags()
{
return $this->belongsToMany(Tag::class);
}
}
// All cached automatically — the query, the eager loads, everything. 🪄
$posts = Post::with('comments', 'tags')
->where('published', true)
->latest()
->paginate(15);
namespace App\Models;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use Cachable;
protected $cachePrefix = 'tenant-123';
}
$result = app('model-cache')->runDisabled(function () {
return MyModel::get();
});
// or via the Facade
use GeneaLabs\LaravelModelCaching\Facades\ModelCache;
ModelCache::runDisabled(function () {
return MyModel::get();
});
namespace App\Models;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
use Cachable;
protected $cacheCooldownSeconds = 300; // 5 minutes ⏱️
}
// Activate using the model's default (300 seconds)
Comment::withCacheCooldownSeconds()->get();
// Or override with a specific duration
Comment::withCacheCooldownSeconds(30)->get();
use GeneaLabs\LaravelModelCaching\Facades\ModelCache;
// Single model
ModelCache::invalidate(App\Models\Post::class);
// Multiple models
ModelCache::invalidate([
App\Models\Post::class,
App\Models\Comment::class,
]);
// In your TestCase setUp() or phpunit.xml
config(['laravel-model-caching.enabled' => false]);
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Model;
/**
* @mixin \GeneaLabs\LaravelModelCaching\CachedBuilder<\Illuminate\Database\Eloquent\Model>
*/
class Post extends Model
{
use Cachable;
}
sh
php artisan modelCache:publish --config
MODEL_CACHE_FALLBACK_TO_DB=true
sh
php artisan modelCache:clear --model='App\Models\Post'
sh
php artisan modelCache:clear
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.