Laravel Tutorials

Laravel Fluent isEmpty and isNotEmpty Methods

Published
Laravel Fluent isEmpty and isNotEmpty Methods image

The Fluent class now includes isEmpty() and isNotEmpty() methods for checking whether an instance contains data.

Previously, checking if a Fluent instance held any data required indirect approaches:

use Illuminate\Support\Fluent;

$fluent = new Fluent(['name' => 'Laravel News']);

if (count($fluent->toArray()) > 0) {
    // Has content
}

if (isset($fluent->name) || isset($fluent->url)) {
    // Has some content
}

The new methods provide direct boolean checks:

$populated = new Fluent(['name' => 'Laravel News', 'url' => 'https://laravel-news.com']);
$empty = new Fluent();

$populated->isEmpty();    // false
$populated->isNotEmpty(); // true

$empty->isEmpty();        // true
$empty->isNotEmpty();     // false

Configuration processing shows practical usage:

use Illuminate\Support\Fluent;

class ConfigurationProcessor
{
    public function processSection(Fluent $config)
    {
        if ($config->isEmpty()) {
            return $this->getDefaultConfiguration();
        }
        
        return $this->buildConfigurationArray($config);
    }
    
    public function mergeConfigurations(array $sections)
    {
        $merged = new Fluent();
        
        foreach ($sections as $section) {
            if ($section->isNotEmpty()) {
                $merged = $merged->merge($section->toArray());
            }
        }
        
        return $merged->isNotEmpty() ? $merged : $this->getDefaultConfiguration();
    }
}

The methods work for conditional logic without array conversion:

if ($configData->isNotEmpty()) {
    $this->processConfiguration($configData);
} else {
    $this->loadDefaultConfiguration();
}

These methods eliminate the need to convert Fluent instances to arrays or check individual properties just to determine if the instance contains any data.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support image

PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support

Read article
Insert Middleware Before or After Existing Laravel Middleware image

Insert Middleware Before or After Existing Laravel Middleware

Read article
Laravel Herd 2: Coding Agents, Workspaces, and Cloud Deploys image

Laravel Herd 2: Coding Agents, Workspaces, and Cloud Deploys

Read article
Strict Request Input Casting in Laravel image

Strict Request Input Casting in Laravel

Read article
Prevent Duplicate File Uploads With Laravel Storage Dedupler image

Prevent Duplicate File Uploads With Laravel Storage Dedupler

Read article
Slow Job Alerts and Replica Commit IDs in Laravel 13.40 image

Slow Job Alerts and Replica Commit IDs in Laravel 13.40

Read article