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.
Pros:
flagged()/notFlagged() scopes enable efficient bulk operations, critical for scaling.Cons:
flags table, which may complicate polymorphic relationships or STI hierarchies.spatie/laravel-model-flags and Laravel’s core.flags table with model_id, name, and timestamps.vendor:publish).flags table. Benchmark with DB::enableQueryLog() to validate impact on high-traffic endpoints.migrate execution (test in staging first).user_* prefixes) to avoid clashes.settings tables.User::flagged('x')->count() on large datasets.flags table metadata?)spatie/laravel-activitylog (for audit trails) or spatie/laravel-medialibrary (for metadata).SendPromotionMail).User, Order).user_verified_email, order_shipped).php artisan vendor:publish --tag="model-flags-migrations" in a staging DB.use Spatie\ModelFlags\Models\Concerns\HasFlags;
class User extends Model { use HasFlags; }
flag_model.hasFlag(), flag(), and scopes.flags table deletion (if needed).php -v before integration).deleted() if needed.Cache::remember()).TestModel).User flags for feature rollouts).flags.name if querying by flag name frequently.Cache::forever() for static flags).flags table growth (alert if >1M rows).DB::listen()).Flag::where('updated_at', '<', now()->subYear())->delete()).FlagExists exception or use try-catch for idempotent operations.flags table grows >10GB.with() to eager-load flags.DB::enableQueryLog() to inspect flag-related queries.$user = User::first();
$user->flag('test'); $user->hasFlag('test'); // true
Flag::truncate()).flags table exceeds 100M rows, consider sharding by model_id ranges.flags table size; optimize indexes if queries time out.is_feature_enabled).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Database downtime | Flags inaccessible | Use transactions; implement retry logic. |
| Flag table corruption | Lost flags | Regular backups; test restore procedures. |
| Concurrent flag updates | Race conditions (e.g., double flags) | Use database transactions or queues. |
| Unbounded flag proliferation | Table bloat, slow queries | Enforce TTL or cleanup policies. |
| Flag naming collisions | Overwritten flags | Enforce namespacing |
How can I help you explore Laravel packages today?