Skip to main content

What is the Fluent Class?

The Fluent class is a versatile utility class that allows you to work with arrays as if they were objects. Implemented in Illuminate\Support\Fluent, it has existed since Laravel’s early versions but is rarely documented in the official guides. Internally, it manages array data as properties and uses magic methods (__get, __set, __call) to provide property-like read/write access.
With the addition of the fluent() helper function in Laravel 11 and enhancements to the Fluent class itself, now is the perfect time to leverage this powerful utility.

Creating Instances

Constructor

make() Factory Method

fluent() Helper Function

Laravel 11 introduces the fluent() helper function, equivalent to Fluent::make().

Property Access

Dynamic Property Read/Write

Method Chaining

The __call magic method allows you to call non-existent methods to set properties. Each method returns $this, enabling chaining.
This enables a fluent interface for setting values, providing a more elegant alternative to array syntax.

Key Methods

get() — Access Using Dot Notation

set() — Set Values Using Dot Notation

fill() — Set Multiple Properties at Once

all() — Get All Properties as Array

scope() — Convert Nested Values to New Fluent Instance

This allows you to work with nested arrays as separate Fluent objects.

value() — Set Default Values Using Callbacks

Array Operations

toArray() — Convert to Array

getAttributes() — Direct Access to Internal Attributes

ArrayAccess Interface

Fluent implements ArrayAccess, allowing array-like operations.

IteratorAggregate Interface

Fluent can be looped using foreach.

JSON Processing

toJson() — Convert to JSON String

toPrettyJson() — Convert to Formatted JSON

JsonSerializable Interface

Fluent implements JsonSerializable, allowing direct use with json_encode().

State Checking

isEmpty() / isNotEmpty()

Conditionable Trait

Fluent uses the Conditionable trait, supporting conditional operations.
Using when() / unless() methods enables fluent configuration based on conditions.

Macroable Trait

Fluent also uses the Macroable trait, allowing dynamic method addition.

Practical Use Cases

API Response Builder

Configuration Builder

Request Parameter Validation and Transformation

Model and Fluent Integration

Form Data Normalization

Comparison with Other Classes

Fluent vs Array

Fluent vs Model

Internal Implementation Details

The __call method, when no macro is registered, sets the method name as the property key with the provided value and returns $this, enabling method chaining.
Fluent uses the following traits, each providing different capabilities:
  • Conditionablewhen() / unless() for conditional operations
  • InteractsWithData — Data manipulation methods like data()
  • Macroable — Dynamic method addition

Next Steps

Collection Class

Deep dive into the Collection class for working with multiple elements.

Conditionable Trait

Learn to write conditional logic fluently with the Conditionable trait.

Macroable Trait

Learn to dynamically add methods to existing classes with the Macroable trait.
Last modified on July 13, 2026