In this video, we will learn about Fluent Strings in Laravel. Fluent strings provide a more fluent, object-oriented interface for working with string values, allowing you to chain multiple string operations together using a more readable syntax compared to traditional string operations.

There are many Fluent Strings operations. I will show you some of them. First, let's create a new controller:


php artisan make:controller TestController.

Now, go to the TestController and create a function:


public function index()
{
echo '<h1> Fluent Strings</h1>';
}

Create a route:


Route::get('test',[TestController::class ,'index']);

Now, let's see some of the Fluent Strings operations. Start with the after method:

The after method returns everything after the given value in a string. For example:


$slice = Str::of('Welcome to my YouTube Channel')->after('Welcome to');
echo $slice;

This after method returns everything after `Welcome to`. So, it will return `my YouTube Channel`. Let's check:

Go to the browser and add `/test` to the URL and press enter. You can see `my YouTube Channel` here.

Now, let's see the afterLast method:

The afterLast method returns everything after the last occurrence of the given value in a string.


$slice = Str::of('AppHttpControllersController')->afterLast('\');
echo $slice;

Here, in this string, there are 3 slashes. So, the afterLast method just returns this string because this is the last slash.

Let's check. Just refresh the page. You can see it's showing only `controller` ok.

Next method is append:

The append method appends the given values to the string:


$string = Str::of('Hello')->append(' Wolrd');
echo $string;

This append method just appends these two strings `Hello` and `World` ok.

Let's check. Just refresh the page. You can see here the string `World` is appended with `Hello`.

Now, let's see the lower method:

The lower method converts the given string to lowercase:


$result = Str::of('LARAVEL')->lower();
echo $result;

Also, import Str on top as follows:


use IlluminateSupportStr;

Let's check. You can see here the string is in lowercase.

Next method is Replace:

The replace method replaces a given string within the string:


$replaced = Str::of('Laravel 7.x')->replace('7.x', '8.x');
echo $replaced;

You can see here `7.x` replaced with `8.x`.

Next method is title:

The title method converts the given string to Title Case:


$converted = Str::of('this is a title')->title();
echo $converted;

You can see here the string is now in title case.

Now, let's see the Slug method:

The slug method generates a URL-friendly `slug` from the given string:


$slug = Str::of('Laravel Framework')->slug('-');
echo $slug;

You can see now this is a slug ok.

Next method is substr:

The substr method returns the portion of the string specified by the given start and length parameters:


$string = Str::of('Laravel Framework')->substr(8);
echo $string;
$string = Str::of('Laravel Framework')->substr(8, 5);
echo $string;

You can see `Framework` and `Frame`.

Next method is trim:

The trim method trims the given string:


$string = Str::of('/Laravel/')->trim('/');
echo $string;

You can see the result here.

Now, let's see the upper method:

The upper method converts the given string to uppercase:


$adjusted = Str::of('laravel')->upper();
echo $adjusted;

You can see here the `Laravel` text in uppercase ok.

In this way, you can use Fluent strings in Laravel 8.