How to Convert String to CamelCase in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

Hey Friends,

This tutorial shows you How to convert string to camelCase in laravel. I’m going to show you about laravel string camelcase example. I would like to share with you laravel str camelcase. you can see how to convert string to camelcase in laravel.

The Str::camel() method in Laravel is used to convert a string to camel case. It converts a string that is separated by underscores, hyphens, or spaces into camel case by capitalizing the first letter of each word (except for the first word). Here's an example:

In this example, Str::camel() converts the string 'hello_world' into 'helloWorld' by removing the underscore and capitalizing the first letter of the second word.

let's see the simple example:

Laravel String to CamelCase in Controller

you can use it with controller like this way:

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Str;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$string = "hello_world";

$newString = Str::camel($string);

dd($newString);

}

}

Output:

helloWorld

Laravel String to CamelCase in Blade File

you can use it with blade file like this way:

Blade File Code:

<p>{{ Str::camel('hello_world') }}</p>

Output:

helloWorld

I hope it can help you...

Tags :
Shares