colbeh/consts
Tiny Laravel/PHP helper for defining and accessing class constants as “const sets.” Simplifies organizing enums-like values, retrieving constant lists, and keeping shared values centralized in a clean, reusable API.
colbeh/consts) generates PHP constants for Laravel database tables (e.g., users, posts), mapping column names to class-level constants (e.g., UserTable::COLUMN_NAME). This is useful for:
Schema::getColumnListing()). Best used as a complementary layer for explicit column definitions.app/Consts/UserTable.php) with no runtime dependencies beyond Laravel’s core. Can be integrated via:
composer require colbeh/consts.AppTable vs. UserTable).php artisan or CI/CD). Example workflow:
php artisan const:generate
User::where(UserTable::COLUMN_EMAIL, '...')).Rule::in(array_column(UserTable::class, fn($c) => $c->name))).return response()->json([UserTable::COLUMN_ID => $user->id])).| Risk Area | Mitigation Strategy |
|---|---|
| Schema Drift | Constants become stale if tables/columns are modified without re-running the generator. Solution: Tie generation to migrations or use a pre-deploy hook. |
| Naming Collisions | Default naming (TableName::COLUMN_*) may conflict with existing constants. Solution: Customize the generator’s output directory/namespacing. |
| Performance Overhead | Generating constants at runtime (if not cached) could slow deployments. Solution: Cache generated files or pre-generate in CI. |
| Limited Use Cases | Only works for MySQL/PostgreSQL (assumes PDO). Solution: Verify compatibility with your DBAL or extend the package. |
| No Runtime Validation | Constants are static; runtime checks (e.g., Schema::hasColumn()) are still needed. Solution: Combine with Laravel’s schema tools. |
snake_case (e.g., column_name) or UPPER_CASE (e.g., COLUMN_NAME) constants?Schema facade or a custom trait (e.g., HasTableConstants) achieve similar goals with less overhead?Rule::in()).laravel-shift/blueprint) if needed.users) to test the generator.post-migrate scripts or CI pipeline (e.g., GitHub Actions)..github/workflows/deploy.yml snippet:
- name: Generate Constants
run: php artisan const:generate
composer.json constraints). May need adjustments for older versions (e.g., Facade changes).Schema::getColumnListing() works.php artisan vendor:publish --tag=consts
bootstrap/cache/) to avoid regeneration on every request.App\\Consts\\Tables\\UserTable).php artisan const:generate after local migrations.UserTable::COLUMN_EMAIL").// In a migration file
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('email')->unique();
});
// Post-migration: php artisan const:generate
Schema::getColumnListing() with generated constants).app/Consts/).php artisan const:generate."App\\Consts\\Tables\\UserTable::COLUMN_NAME in queries."--ignore flag (if supported).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Generator skips a table | Missing constants for queries | Add logging to the generator. |
| Schema changes without regen | Stale constants | Enforce pre-deploy constant checks. |
| Naming conflicts | Broken autoloading | Use unique namespaces (e.g., App\\Consts). |
| Database connection issues | Generator fails silently | Add error handling to Artisan command. |
| IDE misconfiguration | No autocompletion | Share IDE settings (e.g., PHPStorm paths). |
where(), select()).How can I help you explore Laravel packages today?