to_route() and str() helper functions in Laravel 9

Aug 25, 2022 . Admin

Hello Friends,

Hello all! In this article, we will talk about to_route() and str() helper functions in Laravel 9. if you want to see example of laravel 9 to_route example then you are a right place. We will use laravel 9 to_route helper example. In this article, we will implement a laravel 9 str helper function. Follow bellow tutorial step of laravel 9 to_route() and str() helper function.

Laravel 9 now comes with new str() helper function globally that you can use to do string operations fluently just like how you would do with Str::of.In addition to the str() helper, Laravel 9 also comes with a convenient to_route() helper function that you can use instead of redirect()->route() for named routes for instance.

So let's start with following examples:

Example: str() helper:
OLD Way with Str:
use Illuminate\Support\Str;

$input = 'this is test file.php';

$output = Str::of($input)
                ->replaceLast('php', 'html')
                ->camel();

// thisIsTestFile.html
New Way with srt() helper:
$input = 'this is test file.php';

$output = str($input)
                ->replaceLast('php', 'html')
                ->camel();

// thisIsTestFile.html
Example: to_route() helper: OLD Way with Redirect:
Route::get('redirectHome', function() {
    return redirect()->route('home');
});
New Way with to_route() helper:
Route::get('redirectHome', function() {
    return to_route('home');
});

now it works...

I hope it can help you...

#Laravel