laracraft-tech/laravel-useful-traits
Laravel package with handy daily-use additions: traits for PHP 8.1+ enums (get names/values/array) and Eloquent query scopes (e.g., select all columns except specific ones, date-based scopes), plus an Artisan db:truncate command.
UsefulEnums, UsefulScopes) aligns well with Laravel’s Eloquent ecosystem, enabling non-intrusive adoption without modifying core classes. Traits like selectAllBut and date-based scopes (fromToday, fromYesterday) address recurring query patterns, reducing boilerplate in repositories/services.UsefulEnums bridges a gap in Laravel’s native PHP 8.1+ enum handling, providing utility methods (names(), values(), array()) that are critical for migrations, validation, and dynamic UI generation. This is particularly valuable for teams using enums for state machines or configuration.db:truncate command offers a scalable alternative to manual table truncation, with configurable options (e.g., foreign key checks, selective truncation). This is useful for CI/CD pipelines or local development reset scripts.composer require), and traits are opt-in (no forced overrides). The db:truncate command requires no setup beyond publishing config (optional).selectAllBut caches column names, which requires manual cache clearing after migrations. This introduces a maintenance overhead but is mitigated by clear documentation.RefreshDatabaseFast is deprecated in favor of Laravel’s built-in RefreshDatabase, reducing immediate risk but requiring migration planning if the team relies on it.selectAllBut’s caching mechanism could delay schema changes if not cleared post-deployment. The db:truncate command may impact foreign key constraints if misconfigured (e.g., disabling checks inadvertently).UsefulScopes in repositories but allow db:truncate for local use only?)selectAllBut cache invalidation in CI/CD? (e.g., automate cache clearing post-migration?)UsefulEnums replace custom enum utilities, or coexist with them? (e.g., performance comparisons for large enums.)db:truncate? (e.g., audit logs.)RefreshDatabaseFast users migrate to Laravel’s native trait immediately, or phase it out over releases?UsefulScopes and UsefulEnums.db:truncate or deprecated RefreshDatabaseFast.UsefulScopes.UsefulEnums in migrations/validation.TRUNCATE TABLE scripts with db:truncate in CI/CD or local post-deploy hooks.RefreshDatabaseFast, parallel-test Laravel’s native RefreshDatabase and migrate tests incrementally.selectAllBut cache (e.g., php artisan cache:clear in deployment pipeline).selectAllBut or date scopes.PaymentType::array() vs. custom array() methods).| Phase | Action | Dependencies |
|---|---|---|
| Pre-Integration | Audit existing enum/query patterns for conflicts. | None |
| Trait Adoption | Apply UsefulScopes to repositories; replace custom enum methods. |
PHP 8.1+, Laravel 11+ |
| Command Rollout | Replace truncation scripts with db:truncate in CI/CD. |
Database schema stability |
| Testing | Validate selectAllBut cache behavior post-migration. |
Migration pipeline |
| Deprecation | Migrate from RefreshDatabaseFast to native trait. |
Test suite coverage |
selectAllBut cache (e.g., via php artisan cache:clear or custom command).UsefulScopes/UsefulEnums deprecations in future Laravel versions.db:truncate error handling for large databases (e.g., timeouts, transaction rollbacks).selectAllBut vs. explicit select().array() for large enums).db:truncate safety flags (e.g., --dry-run).selectAllBut returning incorrect columns (cache stale?).db:truncate failing on foreign keys (check --disable-foreign-checks).selectAllBut: Cache invalidation adds O(n) overhead per migration. Mitigate by:
db:truncate: Scales poorly for multi-TB databases. Consider:
DELETE for tables with complex constraints.UsefulScopes in new repositories to reduce query sprawl.UsefulEnums.| Component | Failure Scenario | Mitigation |
|---|---|---|
selectAllBut |
Cache stale after migration. | Automate cache clearing; add pre-deploy checks for schema changes. |
db:truncate |
Foreign key constraint violation. | Use --disable-foreign-checks cautiously; test in staging. |
UsefulEnums |
Performance degradation with large enums. | Benchmark; avoid array() for enums >100 values. |
| Deprecated Trait | RefreshDatabaseFast data leakage in tests. |
Migrate to native trait; add test coverage for edge cases. |
| Cache Bloat | Unbounded column name caching. | Set TTL or implement size-based eviction. |
db:truncate flags.UsefulScopesHow can I help you explore Laravel packages today?