laravel/framework · error · InvalidArgumentException

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

Error message

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

What it means

Thrown by Arr::integer() when the resolved dot-notation value is not an int. is_int() is strict: a numeric string '42' or a float 42.0 will fail (unlike the Cache::integer path which coerces).

Solutions

  1. Pass an int default: Arr::integer($data, 'key', 0).
  2. Cast the source: (int) $data['count'] before lookup.
  3. Use Arr::get() plus (int) cast if string-number inputs are valid.

Example fix

// before
$id = Arr::integer($input, 'id'); // '123' string throws
// after
$id = Arr::integer($input, 'id', 0);
Defensive patterns

Strategy: validation

Validate before calling

$raw = Arr::get($data, 'id');
if (! is_int($raw)) {
    $raw = is_numeric($raw) ? (int) $raw : 0;
}
// or pass a default.

Type guard

function isStrictInt(mixed $value): bool { return is_int($value); }

Try / catch

try {
    $id = Arr::integer($input, 'id');
} catch (\InvalidArgumentException $e) {
    $id = 0;
}

Prevention

When it happens

Trigger: Calling Arr::integer($data, 'key') where the value is a string digit, float, bool, null, or array.

Common situations: JSON/API input where numbers arrive as strings ('"id":123' decoded as string); query params that are always strings; missing keys.

Related errors


AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11). Data as JSON: /api/errors/706795f9c320d8ea. Report an issue: GitHub.

Appendix: 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 e0f6eb3518)