Laravel filter(remove) null and empty array values from Request

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Request Data: How to Filter Null and Empty Values in Laravel

As developers working with APIs, one of the most common hurdles is dealing with inconsistent or messy input data. When handling incoming requests in a Laravel application, you often receive data where fields might be intentionally omitted (null), sent as empty strings (""), or provided as empty arrays ([]). Properly sanitizing this data before processing it—especially when mapping it to Eloquent models—is crucial for preventing runtime errors and ensuring clean database integrity.

This post will walk you through the most robust, developer-focused methods in PHP to filter out null and empty array values from your request data, moving beyond simple string checks.

The Challenge: Messy Input Data

Let’s examine the scenario you presented. You are receiving a JSON payload where some fields might be missing or null, and others contain arrays that need to be handled gracefully.

Here is an example of the raw input data structure you are dealing with:

{
    "user_id": 13,
    "location": null,
    "about": "This Is About the user",
    "avatar": [],
    "users": [
        {
            "user_name": "John",
            "age": "30"
        },
        {
            "user_name": "Jessy",
            "age": "30"
        }
    ]
}

The goal is to process this data, ensuring that keys with null or empty array values are excluded from further processing.

Why Simple Filtering Fails

You attempted to use $Data = array_filter($userRequestData, 'strlen');. While clever for filtering strings based on length, this approach fails when dealing with mixed types:

  1. Null Values: array_filter generally handles null values by treating them as false, but it doesn't offer a universally clean way to handle arrays versus strings simultaneously.
  2. Arrays: When an array is present, checking its length via strlen() throws errors or produces incorrect results if the value itself is an array rather than a string.

To achieve true robustness, we need a custom filtering mechanism that checks the type and content of each value.

The Robust Solution: Custom Recursive Filtering

Instead of relying on a single callback like 'strlen', the most reliable approach is to implement a recursive function or a dedicated helper method that explicitly checks if a value is empty, null, or an empty array. This ensures that we handle strings, arrays, and nested structures correctly, which is a core principle in building resilient services, as emphasized by best practices within the Laravel ecosystem.

Here is how you can implement this filtering logic:

function filterEmptyValues(array $data): array
{
    $filteredData = [];
    foreach ($data as $key => $value) {
        // 1. Check for null values
        if ($value === null) {
            continue;
        }
        
        // 2. Check for empty arrays
        if (is_array($value) && empty($value)) {
            continue;
        }

        // 3. Check for empty strings (optional, but good practice if you only want non-empty strings)
        if (is_string($value) && trim($value) === '') {
            continue;
        }

        // If the value passes all checks, keep it
        $filteredData[$key] = $value;
    }
    return $filteredData;
}

// Example Usage:
$requestData = [
    "user_id" => 13,
    "location" => null,       // Should be removed
    "about" => "This Is About the user",
    "avatar" => [],            // Should be removed (empty array)
    "users" => [              // This array should be kept, as it contains data
        ["user_name" => "John", "age" => "30"],
        ["user_name" => "Jessy", "age" => "30"]
    ]
];

$cleanedData = filterEmptyValues($requestData);

print_r($cleanedData);

Code Explanation

  1. Null Check: The first step explicitly checks if $value is null. If it is, we immediately skip it using continue.
  2. Empty Array Check: We use is_array($value) && empty($value) to specifically target arrays that contain no elements. This correctly handles your "avatar": [] scenario.
  3. String Check (Optional Refinement): We also added a check for empty strings using trim($value) === ''. This is useful if you want to remove fields that are explicitly sent as empty strings ("").

By implementing this custom logic, we gain granular control over what constitutes an "empty" value, ensuring that complex nested data structures, like your "users" array, are preserved while stray nulls and empty collections are safely removed. This level of control is essential when building robust APIs using Laravel.

Conclusion

Filtering request data effectively requires moving beyond simple built-in functions and embracing explicit conditional logic. By creating a dedicated function to check for null, empty arrays, and empty strings, you create a highly reliable mechanism for sanitizing input. This practice ensures that your application handles inconsistent API inputs gracefully, leading to cleaner code, fewer bugs, and more predictable data processing—a hallmark of high-quality development in the Laravel community.