Laravel 9 to_route() and str() Helper Function

Websolutionstuff | Oct-03-2022 | Categories : Laravel

In this article, we will see laravel 9 to_route() and str() helper function. The to_route function generates a redirect HTTP response for a given named route. laravel 9 provide a new helper function to_route() function. Also, provides str() function.

Laravel 9 now comes with a new str() helper function globally that you can use to do string operations fluently just like how you would do with Str::of.

So, let's see to_route() helper function in laravel 9, str() helper function in laravel 9.

Example: to_route() 

Laravel 9 also comes with a convenient to_route() helper function that you can use instead of redirect()->route() for named routes for instance.

// Pre Laravel 9
Route::get('home', function() {
    return redirect()->route('home');
});

// Post Laravel 9
Route::get('home', function() {
    return to_route('home');
});

If necessary, you may pass the HTTP status code that should be assigned to the redirect and any additional response headers as the third and fourth arguments to the to_route method.

return to_route('users.show', ['user' => 1], 302, ['X-Framework' => 'Laravel']);

 

 

Example: str()

The str function returns a new Illuminate\Support\Stringable instance for the given string.

// pre laravel 9

use Illuminate\Support\Str;

$input = 'this is laravel 9 example';

$output = Str::of($input)
                ->replaceLast('example', 'str function')
                ->snake();

// this_is_laravel_9_str_function

// post Laravel 9

$input = 'this is laravel 9 example';

$output = str($input)
                ->replaceLast('example', 'str function')
                ->snake();

// this_is_laravel_9_str_function

This function is equivalent to the Str::of method. 

$string = str('Laravel')->append(' str function');
 
// 'Laravel str function'

If no argument is provided to the str function, the function returns an instance of Illuminate\Support\Str.

$snake = str()->snake('LaravelFramework');

// 'laravel_framework'

 


You might also like :

Recommended Post
Featured Post
How To Live Search In Laravel 9 Using Meilisearch
How To Live Search In Laravel...

In this article, we will see how to live search in laravel 9 using Meilisearch. Here we will learn live search in l...

Read More

Dec-13-2022

Laravel 8 Eloquent whereHas Condition
Laravel 8 Eloquent whereHas Co...

In this example we will see laravel 8 eloquent whereHas() condition. you will learn about wherehas() condition in l...

Read More

Oct-06-2021

How To Validate Multi Step Form Using jQuery
How To Validate Multi Step For...

In this article, we will see how to validate multi step form wizard using jquery. Here, we will learn to validate t...

Read More

Jan-27-2023

Laravel 8 Eloquent orWhereHas Condition
Laravel 8 Eloquent orWhereHas...

In this example we will see laravel 8 eloquent orWhereHas() condition. In previous example we will learn about ...

Read More

Oct-15-2021