mpstenson/laravel-advanced-string
Extends Laravel's Str with a richer set of chainable string helpers—advanced case conversion, truncation, pattern checks, and more—so you can write cleaner, more expressive string manipulation code without building custom utilities.
Install with Composer; the package is auto-discovered on Laravel 9+ (no manual provider registration required):
composer require mpstenson/laravel-advanced-string
Where to look first
README.md — the authoritative method list and signatures. The package is a thin layer over Illuminate\Support\Stringable, so the README is effectively the API reference.src/ — the service provider (*ServiceProvider.php) is where macros/Stringable methods are registered; read it first if a documented method doesn't appear to exist in your version.tests/ — the test suite doubles as executable documentation for edge cases (empty strings, multibyte input, nulls) that the README doesn't spell out.First use case — chaining onto a fluent string in a controller or service:
use Illuminate\Support\Str;
$normalized = (string) Str::of($request->input('title'))
->squish()
// ... package-provided advanced methods here
->toString();
Verify the concrete method names against the README for your installed version rather than guessing — the package's value is in the added methods, and the exact names are the contract.
Version note: 1.1.1 is the first release with Laravel 12 support. If you are on Laravel 12, require ^1.1.1 explicitly; older constraints will resolve to a version whose illuminate/support range excludes v12 and fail at install time.
1. Fluent pipeline, single exit point
Keep the chain pure and cast once at the end. Stringable is immutable — every call returns a new instance — so intermediate results must be reassigned:
$slug = Str::of($post->title)->squish()->lower()->toString();
2. Centralize in a helper or a custom cast
Rather than repeating chains across controllers, wrap the common transformations:
namespace App\Support;
use Illuminate\Support\Str;
class Text
{
public static function headline(?string $value): string
{
return (string) Str::of($value ?? '')->squish();
}
}
For model attributes that are always normalized on read/write, an Eloquent custom cast is the cleaner seam:
protected function casts(): array
{
return ['title' => HeadlineCast::class];
}
3. Form request normalization
Run input through the package's string methods in prepareForValidation() so validation rules see canonical data (trimmed, collapsed whitespace, consistent casing). This avoids duplicated trim() calls in every rule and keeps the controller thin.
4. Blade and API resources
Because Stringable implements __toString(), results can be interpolated directly in Blade ({{ Str::of($name)->squish() }}). For API output, prefer converting to a primitive in the resource layer so the JSON contract stays a string, not an object that happens to serialize.
5. Extend rather than fork
If you need behavior the package doesn't ship, register a macro on Stringable in your AppServiceProvider::boot():
use Illuminate\Support\Stringable;
Stringable::macro('initials', function (): Stringable {
// ...
});
This composes with the package's methods in the same chain and survives package upgrades.
6. Testing
Assert on final strings, not intermediate Stringable instances. assertSame against a literal expected value is the most durable test; snapshotting a whole chain tends to break on unrelated formatting changes.
composer.json constraint. If your composer.json pins laravel/framework: ^12.0, the resolver will pick 1.1.1 — but if you have a lockfile from a pre-1.1.1 install, run composer update mpstenson/laravel-advanced-string rather than assuming composer install picks it up.dependabot/fetch-metadata and aglipanci/laravel-pint-action upgrades affect the package's GitHub Actions, not your runtime. Don't go looking for behavioral changes in 1.1.1.Str::of(null) yields an empty string, but a method that internally calls a PHP function which rejects null will throw once the chain is evaluated. Guard optional columns with ?? '' before entering the chain if the input is genuinely nullable.mb_* functions. Test with accented characters and emoji — a strlen-based implementation silently corrupts UTF-8 and the bug won't surface in English-only fixtures.Stringable is immutable. Forgetting to reassign ($s->method(); with no assignment) is the most common silent failure. Static analysis (larastan/phpstan with checkUnusedFunctionCalls) catches this class of bug cheaply.php artisan config:clear after changing it locally — a stale bootstrap/cache/config.php will make new registrations appear broken.->dd() and ->dump() exist on Stringable and are far faster than breaking the chain into temporaries. Drop ->dump() mid-chain to see the value at that exact step."mpstenson/laravel-advanced-string": "^1.1.1" in require so teammates and CI resolve a Laravel-12-compatible version on a fresh install.Stringable::macro() for one-off helpers, a custom cast for model-bound normalization, and a dedicated support class for multi-step business formatting. Reach for a fork only if the package's method semantics conflict with yours.How can I help you explore Laravel packages today?