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.
Installation
composer require colbeh/consts
Publish the configuration (if needed):
php artisan vendor:publish --provider="Colbeh\Consts\ConstsServiceProvider"
First Use Case
Generate constants for a database table (e.g., users):
php artisan consts:generate users
This creates a UsersConst class in app/Consts/ with column names as constants (e.g., UsersConst::NAME).
Where to Look First
app/Consts/ for auto-generated classes.config/consts.php for customization (e.g., namespace, output path).php artisan to see available commands (consts:generate, consts:clear).Table-Driven Development
$user = User::where(UsersConst::STATUS, UsersConst::ACTIVE)->first();
php artisan consts:generate users --force.Integration with Eloquent
// Before
User::where('status', 'active')->get();
// After
User::where(UsersConst::STATUS, UsersConst::ACTIVE)->get();
API/Request Validation
public function rules()
{
return [
UsersConst::EMAIL => 'required|email',
UsersConst::STATUS => 'in:'.UsersConst::ACTIVE.','.UsersConst::INACTIVE,
];
}
Seeding Data
User::create([
UsersConst::NAME => 'John Doe',
UsersConst::STATUS => UsersConst::ADMIN,
]);
'naming' => [
'class' => 'UserTable',
'constants' => 'SNAKE_CASE',
],
php artisan consts:generate users --columns=name,email,status
$column = request('column');
$value = request('value');
User::where($column, $value)->get(); // Replace $column with UsersConst::COLUMN if known.
Namespace Collisions
app/Consts/ is excluded from autoloading if using custom namespaces.Consts/ to composer.json under "autoload-exclude".Database Schema Changes
php artisan consts:generate users --force
--force to avoid stale constants.Reserved Words
class, table, or PHP keywords.user_class → UsersConst::USER_CLASS).Case Sensitivity
UPPER_SNAKE_CASE by default. Adjust in config if needed.php artisan consts:generate users --verbose
php artisan config:clear
app/Consts/ (e.g., PHPStorm: File > Invalidate Caches).Custom Generators
php artisan vendor:publish --tag=consts.templates
app/Consts/ConstGenerator.php.Post-Generation Hooks
consts.generated event in EventServiceProvider:
public function boot()
{
Consts::generated(function ($table, $class) {
// Run custom logic (e.g., log, notify)
});
}
Multi-Tenant Support
php artisan consts:generate users --tenant=acme
--no-backup: Skip backup files for CI/CD pipelines:
php artisan consts:generate users --no-backup
How can I help you explore Laravel packages today?