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.
flags table, isolating state from core model data.flagged(), notFlagged()) for efficient bulk operations, leveraging Laravel’s query builder.hasFlag() or flag() operation triggers a database query. For high-frequency checks (e.g., per-request), consider caching (e.g., Redis) or batching.flags table. Mitigate with:
DB::transaction() or optimistic locking ($model->fresh()).flags table, which may conflict with existing projects using similar patterns (e.g., model_state tables). Audit for overlaps pre-integration.is_processed) or augmenting them? If replacing, assess migration effort.lastFlaggedAt()) or just binary state?flags:array) might be simpler.Flag::where()->delete()) impact performance during maintenance?flagged_at column to the flags table for auditing.flags table schema for DBAs.snake_case) to avoid collisions?jsonb vs. MySQL’s json).HasFlags trait for unit tests:
$model->shouldReceive('flags')->andReturn(new FlagCollection());
status enums). Identify candidates for flag replacement.whereNull('processed_at')) to estimate idempotency gains.User) and 2–3 flags for a proof-of-concept.is_email_verified column with hasFlag('email_verified').php artisan vendor:publish --tag="model-flags-migrations").use HasFlags to target models. For existing records, seed flags via a seeder or data migration:
User::where('is_email_verified', true)->get()->each(fn($u) => $u->flag('email_verified'));
// Before: User::whereNull('processed_at')->get();
// After: User::notFlagged('processed')->get();
User::notFlagged('sent_invoice')
->chunk(100, fn($users) => Invoice::send($users)->each(fn($u) => $u->flag('sent_invoice')));
is_deprecated = true to columns).laravel-activitylog) if using their flagging patterns.Flagged/Unflagged), but requires custom event listeners.HasFlags in legacy systems:
interface Flaggable { public function flag(string $name): void; }
flags table if querying by name or flaggable_id:
Schema::table('flags', function (Blueprint $table) {
$table->index(['name', 'flaggable_id']);
});
flags table is self-contained but requires manual updates for new columns (e.g., adding description to flags). Use migrations for changes.flags table in database backups. Consider point-in-time recovery for critical flags.Flag::where('updated_at', '<', now()->subYears(2))->delete();
deleted_at column to the flags table and override the delete() method in the Flag model.flag_model in config/model-flags.php if extending the Flag model (e.g., adding metadata).HasFlags and the flags table exists. Check for transaction rollbacks.DB::enableQueryLog() to profile flag queries. Optimize with:
User::notFlagged('x')->get()).$hasFlag = Cache::remember("user_{$user->id}_flag_receivedMail", now()->addHours(1), fn() => $user->hasFlag('receivedMail'));
flaggable_id and flaggable_type columns in the flags table match your model’s primary key and class name.domain_process).flags table is replicated synchronously.How can I help you explore Laravel packages today?