Str::camel

Converts a snake_case, kebab-case or spaced string to camelCase.

Signature
Str::camel(string $value): string

Parameters

ParameterTypeDescription
$$valuestringThe string to convert. Hyphens, underscores and runs of whitespace are treated as word boundaries.

Return value

stringThe value converted to camelCase: hyphens, underscores and whitespace removed, each following word capitalised, and the very first character lower-cased.

Errors

Nothing.

Str::camel is lcfirst(Str::studly($value)). studly() replaces - and _ with spaces, splits on whitespace, upper-cases the first letter of each piece and joins them; camel then lowers the very first character.

When to reach for it

Use it at boundaries where naming conventions differ: mapping snake_case database columns or kebab-case config keys onto camelCase PHP properties or JSON payload keys, or generating a method name such as scopeActiveUsers from a token.

Results are cached

Both camel() and the studly() underneath it memoise by input in a static array, so calling it in a loop over the same handful of keys costs almost nothing after the first pass. The cache is keyed on the raw input string and lives for the process; Str::flushCache() clears it, which is only relevant in long-running workers that generate unbounded distinct inputs.

What trips people up

It only touches the first character's case. It never lowers anything inside a word, so an all-caps input stays all-caps: Str::camel('FOO_BAR') is 'fOOBAR'. Likewise Str::camel('XMLHttpRequest') is 'xMLHttpRequest', because there is no delimiter to split on. Pipe uncertain input through Str::lower() or Str::snake() first.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Map snake_case database columns onto camelCase keys in a JSON API response.
  • Turn a kebab-case config or CLI option name into a PHP property name.
  • Build a dynamic method name such as scopeActiveUsers from a supplied token.

Frequently asked questions

Why does Str::camel leave my string in capitals?

Because it only lower-cases the first character; it never lower-cases letters inside a word. Str::camel('FOO_BAR') is 'fOOBAR'. Call Str::lower() on the input first.

Is Str::camel expensive to call in a loop?

No. Results are memoised per input string in a static cache shared with Str::studly(), so repeated calls with the same value return immediately.

What is the difference between Str::camel and Str::studly?

They are identical except for the first character: studly() returns 'FooBar' and camel() returns 'fooBar'. camel() is literally lcfirst(studly($value)).

More examples

Re-keying a database row for a JSON response

The printed array has keys id, firstName, emailVerifiedAt and isAdmin. Because camel() memoises per input, running this over ten thousand rows performs the conversion once per distinct column name.

Example · php
<?php

use Illuminate\Support\Str;

$row = [
    'id'              => 7,
    'first_name'      => 'Ada',
    'email_verified_at' => '2026-01-04 09:00:00',
    'is_admin'        => false,
];

$payload = collect($row)
    ->mapWithKeys(fn ($value, $key) => [Str::camel($key) => $value])
    ->all();

print_r($payload);

Normalising before converting

The left column is raw camel() output and is unusable for the first two inputs. Lower-casing first gives the intended result, because camel() capitalises word starts but never lowers what is already upper-case.

Example · php
<?php

use Illuminate\Support\Str;

$headers = ['CONTENT_TYPE', 'X-Request-ID', 'accept_encoding'];

foreach ($headers as $header) {
    echo Str::camel($header), ' | ', Str::camel(Str::lower($header)), "\n";
}

// cONTENTTYPE      | contentType
// xRequestID       | xRequestId
// acceptEncoding   | acceptEncoding

Discussion

  • Be the first to comment on this lesson.