How to convert string to `camelCase` in Laravel
The Camel case (sometimes stylized as camelCase
also known as camel caps) is the practice of writing phrases without spaces or punctuation.
The separation of words starts with a single capitalized letter, and the first word starts with lower case. In other words, the first character of all words will be in the upper or capital case except the first word.
To better understand let us take some common examples includes
- “iPhone”
- “eBay”
- “personWeight”
- “statueHeight”
- “birthYear”
- “studentFirstName”
- “studentLastName”
Here the first character of the first word is in the lower case whereas the starting character of the next word is in the upper or capital case joined without any space or punctuation.
Image Source: Wikipedia
In this article, we will see how to convert a given string to camelCase in Laravel.
The Laravel provides a static method named camel
in the Str
class that can be accessed as Str::camel
.
Example for snake_case
use Illuminate\Support\Str;
// The value given to camel method is in snake_case
echo Str::camel('student_first_name');
// studentFirstName
Example for kebab-case
use Illuminate\Support\Str;
// The value given to camel method is in kebab-case
echo Str::camel('student-first-name');
// studentFirstName
Example for StudlyCase
aka PascalCase
use Illuminate\Support\Str;
// The value given to camel method is in StudlyCase aka PascalCase
echo Str::camel('StudentFirstName');
// studentFirstName
Hope you find this helpful!
Keep helping and happy 😄 coding