sakanjo/laravel-easy-eloquent-memoization

Easy eloquent memoization for Laravel Eloquent models.

Maintainers

Package info

github.com/sakanjo/laravel-easy-eloquent-memoization

pkg:composer/sakanjo/laravel-easy-eloquent-memoization

Transparency log

Fund package maintenance!

sakanjo

Statistics

Installs: 7

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.2 2026-08-09 18:06 UTC

This package is auto-updated.

Last update: 2026-08-09 18:08:30 UTC


README

Preview

Workflow status Laravel v11.x PHP 8.2

đŸ”Ĩ Easy eloquent memoization

✨ 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