How can I camelcase a string in php

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Naming Conventions: How to CamelCase Strings in PHP

As developers working with frameworks like Laravel, we constantly encounter situations where database column names or configuration keys arrive in a snake_case format (e.g., where-created_at), but our application logic demands the more readable camelCase convention (e.g., whereCreateAt). The challenge, as you've identified, is creating a robust, reusable method to handle this transformation cleanly within your models or service layers.

This post will dive deep into how you can effectively camelcase strings in PHP, providing practical solutions that fit perfectly into dynamic features like custom search queries, ensuring your code remains clean and maintainable.

Why Custom CamelCasing is Necessary

PHP does not offer a single built-in function specifically named camelCase(). While string functions exist, converting complex delimited strings requires custom logic to handle separators (like hyphens or underscores) correctly. Trying to achieve this with simple replacements often leads to errors when dealing with specific naming rules.

For instance, simply replacing all underscores with capital letters is insufficient; you need logic to correctly capitalize the first letter of every word segment. This necessity pushes us toward writing a dedicated helper function that handles the complexity reliably.

The Developer's Approach: Creating a Robust camelCase Helper

The most professional way to solve this is by creating a static or instance method within your Model or a dedicated helper class, allowing it to be reused across your application. This keeps the transformation logic encapsulated and easy to test.

Here is a practical example of how you can implement this conversion in a strongly-typed environment like Laravel:

class Model
{
    /**
     * Converts a snake_case string to camelCase.
     * Example: 'where-created_at' becomes 'whereCreateAt'
     *
     * @param string $snakeCaseString The input string in snake_case format.
     * @return string The camelCased string.
     */
    public function camelCase(string $snakeCaseString): string
    {
        // 1. Split the string by the delimiter (using underscore or hyphen)
        $parts = explode('_', $snakeCaseString);

        $camelCaseString = '';
        foreach ($parts as $part) {
            // Ensure the part exists and capitalize the first letter
            if (!empty($part)) {
                $camelCaseString .= ucfirst($part);
            }
        }
        return $camelCaseString;
    }

    /**
     * Example usage within a search context.
     */
    public function search(array $for, Model $in)
    {
        $results = [];
        foreach ($for as $column => $value) {
            // Apply the custom camelCase method here
            $camelColumnName = $this->camelCase($column);
            
            // Assuming the model has a dynamic method access (like Eloquent does)
            $results[] = $in->{$camelColumnName}($value)->get();
        }
        return $results;
    }
}

Explanation of the Logic

  1. Splitting: We use explode('_', ...) to break the input string into an array of segments wherever an underscore exists.
  2. Iteration and Capitalization: We loop through these parts. For each part, we use ucfirst() to capitalize the first letter and append it to our result string. This ensures that strings like where-created_at are correctly transformed into whereCreateAt.

This approach gives you full control over the transformation process, making it highly adaptable for different naming schemes used in your database—a principle central to good object-oriented design, much like the principles guiding Laravel's Eloquent structure.

Applying CamelCasing in Your Search Feature

Now, let's see how this custom function solves your original problem within the context of a search query:

If you call the method with $column = 'where-created_at', the camelCase() method will return 'whereCreateAt'. This allows your dynamic search logic to seamlessly interact with Eloquent methods, making your code significantly cleaner than trying to manually map every possible string permutation.

// Example usage based on your requirement:
$in->whereCreateAt('2015-25-12')->get();

By abstracting the naming convention into a dedicated method, you move away from brittle, hardcoded string manipulation and adopt a pattern that promotes code readability and scalability. When building complex features atop Laravel components, prioritizing clear, reusable helpers is key to maintaining a high standard of quality across your entire codebase.

Conclusion

Camelcasing strings in PHP for dynamic feature generation isn't something you find out-of-the-box; it’s a pattern you build. By creating a dedicated helper method that intelligently splits and capitalizes string segments, you achieve robust, predictable results. This practice is essential when working with data abstraction layers, whether you are manipulating Eloquent attributes or structuring complex query builders within your Laravel application. Focus on writing clean, reusable logic, and you'll find that even the most seemingly simple tasks become elegantly solved.