spatie/laravel-model-flags
Add lightweight flags to Eloquent models without extra columns. Set and check flags, then query with handy flagged/notFlagged scopes. Ideal for idempotent, restartable jobs and commands (e.g., send a mail only once per user).
flagged, notFlagged) enable efficient filtering without custom queries, reducing boilerplate.flags), which may violate strict data integrity requirements (e.g., financial systems).->whereJsonContains()) can fail silently if malformed data exists. Mitigation: Validate flag structure in model observers or accessors.flags column could lead to lost updates. Mitigation: Use database transactions or optimistic locking ($model->increment('version')).flags column to existing tables requires downtime if the column is non-nullable. Use nullable() and backfill data.whereJsonContains, which may not leverage indexes effectively. Test with large datasets.flags table (normalized) or a key-value store (e.g., Redis) better suit the use case?spatie/laravel-feature-flags may be better).composer.json constraints). Ensure compatibility with your Laravel version.Spatie\Activitylog\Traits\HasActivity).jsonb vs. MySQL’s json).composer require spatie/laravel-model-flags
Publish config (if needed) and run:
php artisan vendor:publish --provider="Spatie\LaravelModelFlags\LaravelModelFlagsServiceProvider"
use Spatie\ModelFlags\Traits\HasFlags;
class User extends Model
{
use HasFlags;
}
Add the flags column to the database:
Schema::table('users', function (Blueprint $table) {
$table->json('flags')->nullable()->after('updated_at');
});
// Before
User::where('status', 'paid')->get();
// After
User::flagged('paid')->get();
LogEntry) to test performance and edge cases.SendWelcomeEmail command) to validate idempotency.ALTER TABLE users ADD INDEX flags_idx ON flags USING GIN; for PostgreSQL).flag() vs. custom flag() methods).remember()), ensure flags are invalidated correctly.flags column exists and is writable.\Log::debug('Flagged user', ['user_id' => $user->id, 'flags' => $user->flags]);
flags table for high-volume models.jsonb_path_ops).SELECT ... FOR UPDATE).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| JSON column corruption | Flag data loss or query failures | Validate flag structure in model observers. |
| Concurrent flag updates | Lost updates or race conditions | Use database transactions or optimistic locking. |
| Large JSON payloads | Slow queries or timeouts | Limit flag count per model or split into tables. |
| Package version incompatibility | Breaking changes in future updates | Pin version in composer.json and test upgrades. |
How can I help you explore Laravel packages today?