Laravel Tutorials

Laravel Uri toStringable Method

Published
Laravel Uri toStringable Method image

The toStringable() method converts URI objects into Stringable instances, providing direct access to Laravel's string manipulation methods.

Previously, applying string transformations to URI objects required wrapping them with Str::of():

Str::of(Uri::of('http://localhost')->withScheme('https'));

The toStringable() method removes this extra step:

use Illuminate\Support\Uri;

Uri::of('http://localhost')->withScheme('https')->toStringable();

This maintains method chaining from URI construction through string operations:

$endpoint = Uri::of('http://api-service.com')
    ->withPath('/users')
    ->withQuery(['active' => 1, 'limit' => 25])
    ->toStringable()
    ->replace('/users', '/v2/users')
    ->lower()
    ->toString();

URL formatting for logging shows string method integration:

$logUrl = Uri::of('http://example.com/endpoint')
    ->withPath('/api/long/path/resource')
    ->toStringable()
    ->limit(40, '...')
    ->toString();

$masked = Uri::of('https://api.service.com/users/12345')
    ->toStringable()
    ->mask('*', -5, 5)
    ->toString();

The method provides access to all Stringable operations including case conversion, truncation, replacement, and masking without breaking the method chain or requiring helper function wrapping.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Laravel Read Replicas: Check if a Replica Has Caught Up image

Laravel Read Replicas: Check if a Replica Has Caught Up

Read article
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article
Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs image

Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs

Read article
Real-Time Inertia Props With Laravel Reverb image

Real-Time Inertia Props With Laravel Reverb

Read article