DEV Community

Cover image for Laravel 8 String studly() helper function Example
Code And Deploy
Code And Deploy

Posted on

Laravel 8 String studly() helper function Example

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/laravel-8-string-studly-helper-function-example

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;
Enter fullscreen mode Exit fullscreen mode

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

Str::studly('foo_bar')
Enter fullscreen mode Exit fullscreen mode

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>';

    }
}
Enter fullscreen mode Exit fullscreen mode

Laravel Studly Result:

FooBar
CodeAndDeploy
FreeTutorials

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/laravel-8-string-studly-helper-function-example if you want to download this code.

Happy coding :)

Top comments (0)