Laravel Utilities

Convert Variable Names and Slugs to Title Case with Str::headline()

Punyapal Shah 1 min read
edit this tip
Use Str::headline() to convert camelCase, snake_case, or kebab-case strings into clean, human-readable title headings.

When generating automated table headers from database column names (like user_profile_id, is_active, or stripeCustomerId), ucwords() fails on underscores and camelCase boundaries.

Str::headline() converts identifiers into proper Title Case words.

Basic Usage

use Illuminate\Support\Str;

echo Str::headline('user_profile_id');
// Output: "User Profile Id"

echo Str::headline('stripeCustomerId');
// Output: "Stripe Customer Id"

echo Str::headline('send-welcome-email-job');
// Output: "Send Welcome Email Job"

Generating Dynamic Table Headers in Blade

<thead>
    <tr>
        @foreach (['first_name', 'email_address', 'last_login_at'] as $column)
            <th>{{ str($column)->headline() }}</th>
        @endforeach
    </tr>
</thead>

Summary

  • Splits strings on underscores, dashes, and camelCase casing boundaries.
  • Capitalizes each word for clean UI headers and labels.
  • Fluent alternative via str($text)->headline().
Tags: Laravel Strings Formatting Utilities
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips