Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Str Acronym Laravel Package

koenhendriks/laravel-str-acronym

Laravel Str Acronym adds a simple Str::acronym() macro for generating acronyms and initials from strings. Use it for avatars, labels, menus, and compact display names with a clean, chainable Laravel-style API.

View on GitHub
Deep Wiki
Context7

Getting Started

  • Install with composer require koenhendriks/laravel-str-acronym. There is no config to publish and no migration — package discovery registers the service provider that adds the macro to Illuminate\Support\Str.
  • Before installing, check the constraint matrix. Version 1.2.0 supports Laravel 12.x and 13.x and drops Laravel 11. If the app is still on Laravel 11, pin "koenhendriks/laravel-str-acronym": "^1.1" — do not let a composer update silently resolve to 1.2.0 and break the lock file against your framework constraint.
  • Look first at the README for the exact signature (delimiter/options behaviour), then src/ for the service provider — it's a single Str::macro('acronym', …) registration, so the source is the fastest way to confirm edge-case behaviour (multibyte input, single-word input, empty string).
  • First use case: turning a long human label into compact initials — organisation names, department headings, product lines. Typical call sites are avatar fallbacks, table badges, PDF/export headers, and generated slugs or codes.
use Illuminate\Support\Str;

Str::acronym('Koen Hendriks');        // "KH"
Str::acronym('Laravel Str Acronym');  // "LSA"

Implementation Patterns

  • Direct static call is the intended usage. Keep it at the edge (Blade partials, formatters, API resources) rather than deep in models, so the dependency stays swappable.
  • Model accessor for display initials, which is where it earns its keep:
protected function initials(): Attribute
{
    return Attribute::get(fn () => Str::acronym($this->name));
}
  • Wrap it for domain rules. If your product needs to skip connector words ("of", "de", "van", "the") or force a fixed length, put that logic in your own Initials/LabelFormatter class that delegates to Str::acronym() for the base case. Don't fork the macro for one-off requirements; you'll lose upstream fixes.
  • Collections/exports: map it once into a DTO or array rather than calling it repeatedly inside nested loops over large result sets — it's cheap, but the string handling still adds up in CSV/PDF generation over thousands of rows.
  • Testing: assert against Str::acronym() directly in unit tests. Because the macro is registered by the provider, RefreshDatabase-style app boot is enough; you don't need feature-level HTTP tests for formatting logic.
  • Blade: {{ Str::acronym($user->name) }} works, but for repeated use per request consider a small view component so the acronym logic lives in one place and stays testable.

Gotchas and Tips

  • The 1.1.0 → 1.2.0 bump drops Laravel 11 but ships as a minor version. Semver-wise this is a breaking change for anyone on L11. Treat composer update on this package as a deliberate upgrade step: confirm laravel/framework is on 12+ before allowing ^1.2.
  • Laravel 13 support implies a newer PHP floor than you may have assumed. Verify php in composer.json matches your CI matrix before bumping, or the install will fail on the platform check rather than in your code.
  • Macro registration order matters. If you define your own Str::macro('acronym', …) anywhere, it must run after the package provider boots, otherwise the package will overwrite yours. Guard with Str::hasMacro('acronym') in your own provider, or register in a booted() callback.
  • Static analysis and IDE autocompletion won't know the method. Str::acronym() isn't in Laravel's own stubs, so PHPStan/Psalm and PhpStorm will flag it as undefined. Either add a stub file or scope an ignore to the specific call sites — don't blanket-ignore Illuminate\Support\Str calls.
  • Multibyte and accented names are the most likely source of surprises. Confirm empirically whether the package uses mb_* functions for accented or non-Latin input; if not, normalise (or transliterate) upstream before calling it. Write a test with a name containing é, ü, or a hyphenated surname.
  • Empty/whitespace and single-word inputs are worth two explicit tests. These packages typically degrade gracefully, but a returned '' in an avatar fallback is a visible bug, so decide at the call site what the fallback should be.
  • Extension point: the whole package is one macro. If upstream goes quiet, replacing it is a ~10-line provider change — read src/ once so you know exactly what you'd be reimplementing before you depend on it in production.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
ignibyte/aic-framework
glavweb/data-schema-bundle
glavweb/datagrid-bundle
tacticmedia/scim-server
uni-method/frame
neika-ai/sdk
adachsoft/php-code-style
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox