Skip to content

Serialization of 'Closure' is not allowed - Model ::automaticallyEagerLoadRelationships #55354

Closed
@MarekVikartovsky

Description

@MarekVikartovsky

Laravel Version

12.8.1

PHP Version

8.4

Database Driver & Version

No response

Description

Today we implemented new feature Model::automaticallyEagerLoadRelationships(); into AppServiceProvider boot method. When we try to store Model instance into redis, the exception Serialization of 'Closure' is not allowed is thrown.

If I remove automaticallyEagerLoadRelationships from AppServiceProvider, everything works fine.

After quick check, the problem should be in file src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php because the property $relationAutoloadCallback can contain closure, which cannot be serialized.

Steps To Reproduce

  1. Add into boot method in AppService provider Model::automaticallyEagerLoadRelationships();
  2. Retrieve data from database into Model, for example: $data = Language::query()->firstWhere('language', $language);
  3. Try to save it into redis via Cache::put($cacheKey, $data, Carbon::now()->addDay());
  4. Exception Serialization of 'Closure' is not allowed. is thrown

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