DEV Community

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

Posted on

3 2

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 :)

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay