laracraft-tech/laravel-useful-traits
Laravel Useful Additions is a small collection of handy traits and commands for everyday Laravel work, including enum helpers (names, values, array), query scopes like selectAllBut and fromToday/fromYesterday, plus a db:truncate artisan command.
Architecture fit
UsefulEnums, UsefulScopes, RefreshDatabaseFast) plus one Artisan command (db:truncate), a config file, and a package service provider. Nothing here dictates application structure — traits attach at the enum or Eloquent model level, so adoption can be per-class and reversible.Integration feasibility
composer require plus an optional config publish. No database migrations, no service contracts to implement, no data backfill. Integration effort is measured in hours, not sprints.UsefulEnums requires PHP 8.1+; UsefulScopes and db:truncate are version-agnostic within supported Laravel ranges.composer.json constraints.Technical risk
selectAllBut is the highest-risk item. It works by introspecting the table's columns, caching them, then excluding the named columns. The cache is invalidated only when files are added to or removed from the migrations directory — editing an existing migration does not refresh it. That creates a real failure mode: a column added in a modified migration (or added out-of-band) can be silently omitted from selected results after deploy. This needs a mandatory cache-clear step in the deployment runbook or the trait should be barred from hot paths.db:truncate is destructive by design. Without an environment guard and a --force policy, this is a production data-loss vector. It must be treated as a developer/CI tool, not an operational one.RefreshDatabaseFast is deprecated by the maintainer for good reasons (rollback not guaranteed on aborted tests → cross-test data bleed). Do not adopt; use Laravel's built-in RefreshDatabase.names, values, array, selectAllBut, fromToday, fromYesterday are generic method names. Existing model traits or magic-method usage can collide, producing confusing runtime errors.Key questions
UsefulEnums::array() replaces repeated boilerplate, or is this solving a problem we already solved locally?selectAllBut? Explicit column selection is usually the safer default; if this is only for admin scaffolding, it belongs in generator code, not production models.db:truncate intended for local dev only, CI only, or shared staging? Who approves that policy?require (runtime) versus require-dev? db:truncate alone can be dev-only; the traits cannot.cache:clear step we can rely on?Stack fit
UsefulEnums. Best fit is a monolithic or modular Laravel app with broad Eloquent usage and a mature CI pipeline — the enum and truncate features shine there.Migration path
require-dev install first (unlocks db:truncate for local/CI), then promote to require only if we keep UsefulEnums/UsefulScopes in application code. Avoid promoting "just in case."selectAllBut.Compatibility
composer.json constraints before the spike, not after.selectAllBut and db:truncate are the two features where driver behavior matters.Sequencing
UsefulEnums against one real enum set, selectAllBut against one real table, and db:truncate against a local DB. Confirm cache behavior on a simulated column addition.require vs require-dev, ban RefreshDatabaseFast, and write the db:truncate usage policy (local/CI only, environment guard, --force gate).db:truncate in local and CI scripts. Low risk, immediate developer-experience gain, no production surface.UsefulEnums on new enums only (do not retrofit en masse). UsefulScopes::fromToday/fromYesterday on analytics-style read paths with index verification. selectAllBut only if the spike proves the cache-invalidation story is airtight — otherwise defer indefinitely.Maintenance
Support
selectAllBut misfires.Scaling
UsefulEnums scales trivially — pure static reflection, no I/O.fromToday/fromYesterday are date-range predicates; verify they map to indexed columns, otherwise they degrade on large tables exactly like a hand-written whereDate.selectAllBut has the only real scaling concern: one introspection query per uncached table. On a cache miss across dozens of models (fresh deploy, cache flush, new environment), that is a burst of schema queries at startup. Warm the cache post-deploy or keep it off high-traffic paths.db:truncate scales linearly in table count and is fine for local/CI; it is not a production operations tool.Failure modes
db:truncate against the wrong connection or environment → data loss. Mitigation: environment allowlist, explicit --force discipline, never in a deploy pipeline.fromToday/fromYesterday relative to application timezone and DB storage timezone → off-by-one-day results at boundaries.RefreshDatabaseFast → flaky, order-dependent, data-leaking test suites. Mitigation: explicit ban in the ADR and in code review guidance.Ramp-up
selectAllBut, and the cache-invalidation consequence of schema changes. That belongs in a short internal convention note and in the ADR, not in a workshop.UsefulEnums selectively; hold selectAllBut to a documented standard; treat the package as a convenience dependency we are prepared to fork, not as platform infrastructure.How can I help you explore Laravel packages today?