Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Fluent string operations in Laravel 7

Laravel 7 has been a major version that’s been released last week and it comes with the host of features and improvements to the Laravel framework. One of the improvements being the new fluent string operations API. The goal of these API is to provide more flexibility and readablity to the regular string operations in Laravel by chaining array of string manipulation methods.

To give a primer, how Laravel was handling string operations prior to the Laravel 7, we’ll take an example.

Learn more about Fluent Interfaces: Method chaining in PHP in a nutshell

Pre Laravel 7

So, before Laravel 7, we had a set of few string methods provided by Illuminate\Support\Str trait. So, for instance, you want to do something like below, you’d do it like so.

// Generate a file name from the input string
// and change the extension to .html

use Illuminate\Support\Str;

$replaced = Str::replaceLast('php', 'html', 'this is test file.php');

$camelCase = Str::camel($replaced);

// thisIsTestFile.html

As you can see, the above code is a little verbose and though it’s not painfully unreadable, it’s still can be improved. This is where Laravel 7’s fluent API for string operations kicks in.’

Post Laravel 7

Basically, Laravel 7 offers a more object-oriented, fluent string manipulation library built on top of Illuminate\Support\Str functions. In order to use this fluent API, you’d need to use Str::of method which creates a fluent Illuminate\Support\Stringable object. Once done, an array of various string manipulation methods can be chained onto the object. So, the above example can be re-written like so.

// Generate a file name from the input string
// and change the extension to .html

use Illuminate\Support\Str;

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

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

// thisIsTestFile.html

As you can see, the string operations seems far more readable and less confusing. This also reduces the amount of intermediate variable that you’d need to create otherwiese and of course, this would need less number of lines for the equivalent operations.

Here are all the fluent methods that you can utilize by chaining together.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, 8.3, and 8.4), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?