🚀 Introducing Xetaravel-Counts: A Laravel Package to Automatically Maintain Your Relation Counters
When building medium or large Laravel applications — ERP systems, CRMs, business platforms, SaaS dashboards — you often run into the same recurring need: keeping *_count fields in your database in sync with your Eloquent relations.
Typical examples:
- A category needs its articles_count
- A user needs its roles_count
- A material needs its parts_count
- A part needs its materials_count
- A tag needs posts_count etc.
Laravel’s withCount() is great for on-the-fly counting, but in many cases you want a real column stored in the database for:
- much better performance (dashboards, APIs)
- avoiding constant COUNT(*) queries
- sorting and filtering without subqueries
- reporting and exporting
- analytics
👉 That’s exactly why I created Xetaravel-Counts, a lightweight package that automatically keeps your *_count fields up-to-date with zero boilerplate code.
🎯 Why Xetaravel-Counts?
Because doing this manually is painful:
- repetitive observers
- forgetting edge cases (delete, restore, relation changes…)
- risk of race conditions if using manual saves
- duplicated logic across models
With this package, all you need is:
- a trait, and
- a tiny configuration And everything becomes fully automatic.
🔧 Key Features
✅ Automatic handling of belongsTo relationships
Example: Article → Category The trait will automatically:
- increment on creation
- decrement on deletion
- update the old & new parents when the relation changes
- re-increment after
restore()when using SoftDeletes
✅ Automatic handling of belongsToMany relations (pivot)
Example: Material ↔ Part Using a special Pivot trait:
- increments/decrements on attach() and detach()
- handles sync() properly
- suitable for bulk operations
All without writing a single observer.
🔒 Reliable thanks to Atomic SQL updates
The package uses Eloquent’s:
increment()
decrement()
These generate atomic SQL updates like:
UPDATE materials SET parts_count = parts_count + 1 WHERE id = ?;
This avoids concurrency issues and ensures accurate counters even under heavy load.
🚀 Example Usage
belongsTo (Category / Article)
class Article extends Model
{
use HasRelatedCount;
protected static $countedRelations = [
'category' => 'articles_count',
];
}
belongsToMany (Material / Part)
class MaterialPart extends Pivot
{
use HasBelongsToManyCounts;
protected static array $countsConfig = [
'material' => 'parts_count',
'part' => 'materials_count',
];
}
That’s it — no extra logic in your controllers or services.
⚙️ Installation
composer require xeta/xetaravel-counts
No configuration needed — auto-discovery enabled.
📦 Typical Use Cases
- ERP systems (stock, materials, parts, incidents…)
- CMS/blog (posts, comments, tags)
- Marketplaces (categories, products, vendors)
- Social networks (likes, followers, messages)
- SaaS admin panels (tickets, licensing, usage stats)
If your app uses Eloquent relationships, there is a very high chance this package will save you time and prevent bugs.
📚 Full Documentation
Available on GitHub: 👉 https://github.com/XetaIO/Xetaravel-Counts