laravel/framework · error · LogicException

{self::class} may not be mutated using array access.

Error message

{self::class} may not be mutated using array access.

What it means

ModelInfo implements ArrayAccess but is read-only. offsetSet always throws a LogicException because model metadata is computed by the framework and must not be mutated at runtime. Attempting $info['key'] = $value triggers this regardless of the offset.

Source

Thrown at src/Illuminate/Database/Eloquent/ModelInfo.php:105

    /**
     * Get the value for a given offset.
     *
     * @throws \InvalidArgumentException
     */
    public function offsetGet(mixed $offset): mixed
    {
        return property_exists($this, $offset) ? $this->{$offset} : throw new InvalidArgumentException("Property {$offset} does not exist.");
    }

    /**
     * Set the value at the given offset.
     *
     * @throws \LogicException
     */
    public function offsetSet(mixed $offset, mixed $value): void
    {
        throw new LogicException(self::class.' may not be mutated using array access.');
    }

    /**
     * Unset the value at the given offset.
     *
     * @throws \LogicException
     */
    public function offsetUnset(mixed $offset): void
    {
        throw new LogicException(self::class.' may not be mutated using array access.');
    }
}

View on GitHub (pinned to e0f6eb3518)

Solutions

  1. Do not mutate ModelInfo; read its properties instead.
  2. Build a separate array or DTO if you need derived/extra data: $out = array_merge($info->toArray(), ['extra' => 1]).
  3. Construct a new ModelInfo if you genuinely need different values.

Example fix

// before
$info['extra'] = 'value'; // throws

// after
$payload = $info->toArray();
$payload['extra'] = 'value';
Defensive patterns

Strategy: validation

Validate before calling

// Never mutate ModelInfo via array access. Copy to array first.
$payload = $info->toArray();
$payload['extra'] = 'value'; // mutate the copy, not the object

Prevention

When it happens

Trigger: Any assignment through array syntax on a ModelInfo instance: $info['table'] = 'foo' or $info['extra'] = 1.

Common situations: Code that decorates or augments inspection data by writing into the same object; generic serializers that try to add a key; copy-paste from a normal array-access object.

Related errors


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