Table of contents

laravel studly

In this post, I will show you an example of a Laravel 8 string helper called studly. This function will help us to convert any strings to a studly case format. This is useful if you are processing a generated function for a method or class generator or in any text processing. To start with studly() helper function we need to import it to your class.

use Illuminate\Support\Str;

 

Then calling the helper function with Str class. See the below example:

Str::studly('foo_bar')

 

The advantage of this Laravel helper function will support strings that have space, underscore and hypen. See below the actual code example.

 

Laravel Studly Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Str;

class HomeController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        $studly1 = Str::studly('foo_bar');
        
        print_r($studly1);
        // output - FooBar

        echo '<br>';

        $studly2 = Str::studly('code and deploy');
        
        print_r($studly2);
        // output - CodeAndDeploy

        echo '<br>';

        $studly3 = Str::studly('free-tutorials');

        print_r($studly3);
        // output - FreeTutorials

        echo '<br>';

    }
}

 

Laravel Studly Result:

FooBar
CodeAndDeploy
FreeTutorials