Str::toCamelCase()) aligns seamlessly with Laravel’s dependency injection, facade, and helper patterns. It avoids tight coupling, making it ideal for utility-focused Laravel applications.getTable(), getKeyName(), or getFillable().Request validation rules, API payloads, or FormRequest transformations.composer require nayjest/str-case-converter with no Laravel-specific dependencies.$this->app->singleton('caseConverter', function () {
return new \Nayjest\StrCaseConverter\Str();
});
CaseConverter) for intuitive usage:
CaseConverter::toSnakeCase('userFirstName'); // 'user_first_name'
app/Helpers.php for one-liners:
if (!function_exists('to_camel')) {
function to_camel($str) { return \Nayjest\StrCaseConverter\Str::toCamelCase($str); }
}
strict_types, named arguments, or match expressions).Str::of() (v9+) as a fallback.Str::camel()/Str::snake() (v9+) or spatie/array-to-string for active maintenance.null inputs) in Laravel contexts.Str Helper Sufficient?
Str::camel()/Str::snake(), which may replace this package.toSnakeCase(..., '-')), which Laravel lacks.preg_replace_callback.bobthecoder/laravel-helpers.Str::of() with manual delimiters.preg_replace, lcfirst) or Laravel’s Str helpers.table_names vs. TableNames) and namespace collisions.// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton('caseConverter', function () {
return new \Nayjest\StrCaseConverter\Str();
});
}
// app/Facades/CaseConverter.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class CaseConverter extends Facade { public static function getFacadeAccessor() { return 'caseConverter'; } }
Usage: CaseConverter::toCamelCase('snake_string').app/Helpers.php for global access:
if (!function_exists('to_camel')) {
function to_camel($str) { return \Nayjest\StrCaseConverter\Str::toCamelCase($str); }
}
public function test_model_attribute_conversion()
{
$model = new class extends Model { protected $table = 'user_profiles'; };
$this->assertEquals('userProfiles', to_camel($model->getTable()));
}
snake_case ↔ camelCase in models, requests, APIs).Route::resource() model names to snake_case for API routes.str_replace('_', '', $string)) with the package.if (config('app.use_new_case_converter')) {
return CaseConverter::toCamelCase($str);
}
return str_replace('_', '', ucwords($str, '_'));
if (!class_exists(\Nayjest\StrCaseConverter\Str::class)) {
function to_camel_case($str) {
return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $str))));
}
}
getTable(), getKeyName(), or getFillable().snake_case in API payloads ↔ camelCase in DTOs).FormRequest validation rules.creating, updating) to normalize attribute names.protected static function boot()
{
static::creating(function ($model) {
$model->attributes = collect($model->attributes)
->mapWithKeys(fn ($value, $key) => [
CaseConverter::toCamelCase($key) => $value
])
->toArray();
});
}
How can I help you explore Laravel packages today?