Skip to content

Illuminate\Database\Eloquent\Model::offsetUnset is not cleanup classCastCache #56335

@guram-vashakidze

Description

@guram-vashakidze

Laravel Version

12.20.0

PHP Version

8.4.1

Database Driver & Version

MongoDB 8.0.11

Description

The method Illuminate\Database\Eloquent\Model::offsetUnset is not clean up $this->classCastCache[$offset] it's not affect Laravel DB connection drivers from the box (but reprodused), but it's affected official Laravel MongoDB. When I'm using unset method method getDirty() every time returning attribute (which has class cast) which were unset because it's still in classCastCache .

Steps To Reproduce

Can be reproduced on standard Laravel Eloquient DB driver

  1. Create Model
  2. Add class cast on attribute
  3. Prefetch this attribute value
  4. unset this attribute
  5. run (Model)->getAttributes()

Expected result: attribute which were unset must not be in result
Actual result: attribute again appeared in list

This behaviour is changed if change Illuminate\Database\Eloquent\Model::offsetUnset to:

unset($this->attributes[$offset], $this->relations[$offset], $this->attributeCastCache[$offset], $this->classCastCache[$offset]);

My opened issue in MongoDB repo: mongodb/laravel-mongodb#3431

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions