spatie/laravel-model-flags
Add lightweight “flags” to Eloquent models via a trait—store process state without extra columns. Check, set, and clear flags, and query with flagged/notFlagged scopes. Ideal for idempotent, restartable jobs like one-time emails or migrations.
WHERE NOT EXISTS queries).is_processed) with dynamic flags to avoid migrations and enable granular control (e.g., user->flag('beta_access')).User::notFlagged('x')->with('flags')->get()).lastFlaggedAt() to track flag history (e.g., for compliance).User::where('role', 'admin')->flag(['feature_x', 'feature_y'])).Adopt if:
$user->flag('verified') vs. UPDATE users SET verified_at = NOW() WHERE id = ?).Look Elsewhere if:
authorizable or packages like spatie/laravel-permission)."This package lets us add ‘toggle switches’ to any database record—without writing migrations or slowing down queries. For example, we can mark users as ‘eligible for a promotion’ or track which records a batch job has processed, then safely restart the job if it fails. It’s like GitHub Actions’ ‘artifacts’ but for our database: no duplicates, no missed steps, and no schema changes. The cost? Zero—it’s open-source and maintained by Spatie, who build 200M+ downloads of Laravel tools annually."
ROI:
ALTER TABLE for new flags).*"This is a trait-based solution for adding key-value flags to Eloquent models, stored in a separate flags table. Think of it as a lightweight alternative to:
is_verified → hasFlag('verified')).metadata → structured flags with timestamps).Key Benefits:
user->flag('new_feature')).User::flagged('premium')->get()).User::notFlagged('sent_email')->each(...)).flagged() events or add custom logic via the flags() relation.Trade-offs:
Example Use Case:
// In a cron job to send welcome emails:
User::notFlagged('welcome_email_sent')
->chunk(100, function ($users) {
foreach ($users as $user) {
Mail::to($user)->send(new WelcomeEmail());
$user->flag('welcome_email_sent'); // Mark as done
}
});
No more: WHERE email_sent_at IS NULL + manual updates. Just flags.
Next Steps:
User) to measure query impact.HasFlags to 2–3 critical models (e.g., Order, Subscription).FlaggedEvent listener for analytics (e.g., track how often beta_access is toggled).Alternatives Considered:
Recommendation: Adopt for idempotent workflows and feature toggles. Pair with Laravel’s caching for high-read scenarios."*
How can I help you explore Laravel packages today?