laravel/framework · error · InvalidArgumentException

Array value for key [%s] must be an integer, %s found.

Error message

Array value for key [%s] must be an integer, %s found.

What it means

Thrown by Arr::integer() when the value resolved via Arr::get() fails is_int(). The accessor does not coerce numeric strings or floats, so '5', 5.0, true, or null all trigger it.

Source

Thrown at src/Illuminate/Collections/Arr.php:648

            if ($callback($value, $key)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Get an integer item from an array using "dot" notation.
     *
     * @throws \InvalidArgumentException
     */
    public static function integer(ArrayAccess|array $array, string|int|null $key, ?int $default = null): int
    {
        $value = Arr::get($array, $key, $default);

        if (! is_int($value)) {
            throw new InvalidArgumentException(
                sprintf('Array value for key [%s] must be an integer, %s found.', $key, gettype($value))
            );
        }

        return $value;
    }

    /**
     * Determines if an array is associative.
     *
     * An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
     *
     * @param  array  $array
     * @return ($array is list ? false : true)
     */
    public static function isAssoc(array $array)
    {
        return ! array_is_list($array);

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Pass an int default: Arr::integer($data, 'count', 0).
  2. Cast the source: store/normalize with (int) before reading.
  3. Use Arr::get() + (int) cast if numeric-string coercion is desired.
  4. Fix the dot path or schema so the key genuinely contains an int.

Example fix

// before
$n = Arr::integer($payload, 'pagination.total');

// after
$n = Arr::integer($payload, 'pagination.total', 0);
// or coerce
$n = (int) Arr::get($payload, 'pagination.total', 0);
Defensive patterns

Strategy: type-guard

Validate before calling

$value = Arr::get($payload, 'pagination.total');
if (! is_int($value)) {
    $value = (int) $value;
}

Type guard

function arrayKeyIsInt(array $array, string $key): bool {
    return is_int(Arr::get($array, $key));
}

Try / catch

try {
    $n = Arr::integer($payload, 'pagination.total', 0);
} catch (\InvalidArgumentException $e) {
    $n = (int) Arr::get($payload, 'pagination.total', 0);
}

Prevention

When it happens

Trigger: Calling Arr::integer($data, 'count') when the value is the string '5', a float, a bool, an array, or null (missing key with no int default).

Common situations: Decoded JSON where counts come through as strings; a config value that is sometimes null; IDs stored as numeric strings.

Related errors


AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06). Data as JSON: /data/errors/706795f9c320d8ea.json. Report an issue: GitHub.