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
- Pass an int default: Arr::integer($data, 'count', 0).
- Cast the source: store/normalize with (int) before reading.
- Use Arr::get() + (int) cast if numeric-string coercion is desired.
- 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
- Arr::integer() does not coerce — cast numeric strings to int before reading.
- Pass an int default to Arr::integer().
- Decode JSON with JSON_THROW_ON_ERROR and validate counts are ints.
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
- Array value for key [%s] must be an array, %s found.
- Array value for key [%s] must be a boolean, %s found.
- Array value for key [%s] must be a float, %s found.
- Items cannot be represented by a scalar value.
- Array value for key [%s] must be a string, %s found.
AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06).
Data as JSON: /data/errors/706795f9c320d8ea.json.
Report an issue: GitHub.