Using Fluent Method in Laravel

2024-12-03 · 4 min read

What is Fluent Method?What is Fluent Method?

The term fluent method or fluent interface in the programming world is a coding style in the eyes of the method of an object returning itself or another object. So that these methods can be chained in one line of code that is sequential and easy to read.

Fluency in Laravel can be applied in several ways, one of which is using the default Fluent class provided by Laravel.

The Fluent class is a utility class provided by the Laravel framework that allows you to create and manipulate objects such as calling methods or accessing properties in a more expressive and readable way. This fluency method can be used when you have an object with many methods and properties.

What’s wrong with Associative Arrays?What’s wrong with Associative Arrays?

Associative Arrays or Associative Array is a data structure used to store key-value pairs that are commonly used in PHP applications. We will take an example of using an associative arrays to store the attributes of a Product.

$product = [
    'id' => 1,
    'name' => 'Logitech G813',
    'price' => '300000', // IDR
];

To access the product properties, the syntax is as follows:

echo $product['name'];

The above line of code will produce the product name "Logitech G813". Now let’s try to access a property that this product does not have.

echo $product['sku'];

As we know, the sku property does not exist on the product. In this case, PHP will throw an ErrorException with a message like this:

Undefined array key "sku"

Let’s take another example. Assume we define a product as an stdClass instance, not an associative arrays.

$product = (object) [
    'id'    => 1,
    'name' => 'Logitech G813',
    'price' => '300000', // IDR
];

Now let’s try to access the product name using array notation, where the variable is an instance of stdClass:

echo $product['name'];

In this case, PHP will display an error message like:

Cannot use object of type stdClass as array

So, using associative arrays or mixing objects with arrays can sometimes be risky and inefficient. Wouldn’t it be nice if there was an API to access arrays and objects the same way without having to worry about ErrorExceptions or invalid syntax?

Handle with Laravel FluentHandle with Laravel Fluent

With Fluent in Laravel, you can do things more easily than using associative arrays in certain situations. This class lets you access properties on an object using dot notation instead of array notation.

The Fluent class is defined in the Illuminate\Support namespace.

<?php

namespace IlluminateSupport;

class Fluent implements Arrayable, ArrayAccess, Jsonable, JsonSerializable
{
    ...
}

Fluent classes implement Arrayable, ArrayAccess, Jsonable, JsonSerializable interfaces. So, you can access properties using array syntax as well as dot notation. Let’s create some examples of how to use these Fluent classes.

Create Objects Using the make() MethodCreate Objects Using the make() Method

To create an object using the Fluet class, one way is to use the make() method:

use IlluminateSupportFluent;

$product = new Fluent::make([
    'id' => 1,
    'name' => 'Logitech G813',
    'price' => '300000', // IDR
]);

Then you can access the object properties in this way:

echo $product->name;

// or you can also do it with

echo $product['name'];

Create Fluent Object From ArrayCreate Fluent Object From Array

use IlluminateSupportFluent;

$data = [
    'id' => 1,
    'name' => 'Logitech G813',
    'price' => '300000', // IDR
]

$product = new FLuent($data)

Create Fluent Object From Object InstanceCreate Fluent Object From Object Instance

use IlluminateSupportFluent;

$data = new Product(
    id: 1,
    name: 'Logitech G813',
    price: 3000000, // IDR
);

$product = new FLuent($data);

With the Fluent API MethodWith the Fluent API Method

Another method to create a Fluent instance is to use the fluent API like this:

use IlluminateSupportFluent;

$product = (new FLuent)
    ->id(1)
    ->name('Logitech G813')
    ->price('3000000');

Create New Properties with the with() MethodCreate New Properties with the with() Method

For example, you have an array object and it has been made into a Fluent object, then you want to add additional properties. In this case, you can use the with() method like this:

$data = new Product(
    id: 1,
    name: 'Logitech G813',
    price: 3000000, // IDR
);

$product = new FLuent($data)
    ->with('sku', '123ABC');

Object Data ManipulationObject Data Manipulation

use IlluminateSupportFluent;

$data = new Product(
    id: 1,
    name: 'Logitech G813',
    price: 3000000, // IDR
);

$product = new FLuent($data)
    ->with('sku', '123ABC');

$product['price'] = 2500000;

// or

$product->price = 2500000;

ConclusionConclusion

Fluent classes are an essential tool for working with objects in Laravel and are used extensively throughout the framework to provide a more expressive and readable way to work with chained methods.