laravel-easy-eloquent-memoization maintained by sakanjo
Description
Easy eloquent memoization for Laravel Eloquent models.
Author
Last update
2026/08/09 20:06
(dev-master)
License
Downloads
1
Tags

✨ Help support the maintenance of this package by sponsoring me.
Table of Contents
📦 Install
composer require sakanjo/laravel-easy-eloquent-memoization
🦄 Usage
Setup the model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use SaKanjo\EasyEloquentMemoization\Concerns\Memoizable;
class Post extends Model
{
use Memoizable;
}
Query the model
// Total queries made to the database: 1
Post::query()->get(); // will cache the result
Post::query()->get(); // will return the cached result
Post::query()->get(); // will return the cached result
Post::query()->get(); // will return the cached result
Post::query()->get(); // will return the cached result
// Total queries made to the database: 1
Post::count(); // will cache the result
Post::count(); // will return the cached result
Post::count(); // will return the cached result
Post::count(); // will return the cached result
// Total queries made to the database: 4
Post::query()->withoutMemoization()->get(); // will return the result without caching it
Post::query()->withoutMemoization()->get(); // will return the result without caching it
Post::query()->withoutMemoization()->get(); // will return the result without caching it
Post::query()->withoutMemoization()->get(); // will return the result without caching it
That's it!
📚 Functions
withoutMemoization
Post::query()->withoutMemoization()->get();
clearMemoization
$post = Post::query()->first();
$post->clearMemoization(); // will clear the cache for this model
Post::query()->first(); // will query the database again and cache the result
clear all memoized queries
use SaKanjo\EasyEloquentMemoization\MemoizeManager;
MemoizeManager::clear();
💖 Support the development
Do you like this project? Support it by donating
Click the "💖 Sponsor" at the top of this repo.
©️ Credits
📄 License
MIT License © 2023-PRESENT Salah Kanjo