nabil1337/case-helper
case-helper is a Laravel/PHP utility package for converting strings between common case formats like camelCase, snake_case, kebab-case, and StudlyCase. It provides simple, reusable helpers to keep naming consistent across your application.
nabil1337/case-helper is a small, static utility class for converting strings between the seven common case conventions: camelCase, PascalCase, kebab-case, snake_case, SCREAMING_SNAKE_CASE, Train-Case, and raw spaced "string case."
Minimal steps:
composer require nabil1337/case-helperuse it directly.vendor/nabil1337/case-helper/src/ first — it's a single class file. Read the public static method names before writing anything; the exact verb naming (toCamel(), camelCase(), convert()) is the one thing this package doesn't document well.use Nabil1337\CaseHelper\CaseHelper; // confirm namespace from the src file
CaseHelper::snakeCase('Blog Post Category'); // blog_post_category
CaseHelper::kebabCase('Blog Post Category'); // blog-post-category
CaseHelper::pascalCase('blog post category'); // BlogPostCategory
CaseHelper::screamingSnakeCase('Blog Post'); // BLOG_POST
CaseHelper::trainCase('blog post'); // Blog-Post
If the static API is awkward for Laravel, wrap it once (see below) and never call the class directly again — that limits your exposure when the unmaintained package breaks.
Wrap it behind a Laravel helper or Str macro. The package is a pure string transformer; the value in a Laravel app is in normalizing it into the framework's idioms. A macro makes it discoverable and swappable:
// AppServiceProvider::boot()
Str::macro('train', fn (string $s) => CaseHelper::trainCase($s));
Str::macro('screamingSnake', fn (string $s) => CaseHelper::screamingSnakeCase($s));
Then call sites read like native Laravel: Str::train($label).
Model-to-table / class-name conventions. Anywhere you generate artifacts from user input — migrations, models, enum keys, route names — compute all forms in one pass instead of chaining ad-hoc str_replace calls:
$forms = [
'table' => CaseHelper::snakeCase($label),
'slug' => CaseHelper::kebabCase($label),
'class' => CaseHelper::pascalCase($label),
'enum' => CaseHelper::screamingSnakeCase($label),
];
Form request normalization. Convert inbound JSON keys or form field labels to a canonical casing in prepareForValidation() or a middleware, so controllers always receive camelCase:
protected function prepareForValidation()
{
$this->merge(collect($this->all())
->mapWithKeys(fn ($v, $k) => [CaseHelper::camelCase($k) => $v])
->all());
}
API resources / response shaping. Use kebab or snake case on the way out when integrating a third-party API that demands a specific convention, rather than hand-writing key maps in every resource.
Blade + config. Register a Blade directive (@train($label)) only if designers/templates genuinely need it; otherwise keep it server-side. For a repeated convention (e.g. "all slugs are kebab"), codify it in a small NamingService and inject that, so the package is one implementation detail rather than a hard dependency sprinkled across views.
Pair with Str::slug() for URLs. kebabCase() handles casing, not accent stripping or URL safety — run the result through Laravel's Str::slug() before using it in routes.
Str::camel, Str::snake, Str::kebab, and Str::studly all exist in modern Laravel. The only two things this package adds are Train-Case and SCREAMING_SNAKE_CASE. If those are all you need, a 3-line macro on top of Str lets you drop the dependency entirely — strongly consider that first.composer.json constraints that fail composer install on current PHP. Pin the version and expect to replace it within a year.config/app.php providers — there's nothing to register. If you see docs implying otherwise, they're generic boilerplate.HTMLParser, OAuth2Token, userID, and strings with consecutive capitals or digits before relying on round-tripping. Most 2014-era converters mangle acronyms and produce h_t_m_l_parser.camelCase → snake_case → camelCase is not guaranteed to return the original when the input had unusual capitalization. Treat conversions as one-directional and never use this for "decode what the user meant."Str::ascii() or iconv first if your labels are internationalized.CaseHelper::camelCase('') and inputs starting with digits produce edge cases (leading underscores, empty strings). Guard with filled() / a null-coalescing wrapper at call sites.Str macros or spatie/string.app/Support/ is often cleaner than depending on an abandoned package.How can I help you explore Laravel packages today?