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 Advanced String Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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

  1. 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.
  2. 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.
  3. 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.

Implementation Patterns

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.

Gotchas and Tips

  • Laravel 12 requires 1.1.1+. The only functional change in this release is the widened 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.
  • Dependency bumps in these notes are CI-only. The 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.
  • Null propagation. 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.
  • Multibyte input. Any length/substring/index method is only correct if it routes through 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.
  • Config caching. If you register macros or publish config in a service provider, php artisan config:clear after changing it locally — a stale bootstrap/cache/config.php will make new registrations appear broken.
  • Debugging chains. ->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.
  • Pin the constraint, not the lockfile, when sharing. Use "mpstenson/laravel-advanced-string": "^1.1.1" in require so teammates and CI resolve a Laravel-12-compatible version on a fresh install.
  • Extension points to prefer: 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.
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.
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log