Skip to content

Serialization of 'Closure' is not allowed when using Model::automaticallyEagerLoadRelationships() and caching #55378

Closed
@broqit

Description

@broqit

Laravel Version

12.8.1

PHP Version

8.4.5

Database Driver & Version

8.0.31

Description

After adding the following line to the AppServiceProvider’s boot() method:
`use Illuminate\Database\Eloquent\Model;

public function boot(): void
{
Model::automaticallyEagerLoadRelationships();
}`

an error occurs when trying to cache a model collection or API resource:

Serialization of 'Closure' is not allowed

This happens even when caching plain model collections like:

Cache::remember('key', 3600, fn () => Model::with('relation')->get());

or:

Cache::remember('key', 3600, fn () => MyResource::collection(Model::with('relation')->get())->resolve());

Steps To Reproduce

1.	Add Model::automaticallyEagerLoadRelationships(); in your AppServiceProvider::boot()
2.	Attempt to cache an Eloquent model collection or API Resource
3.	See this error:

Serialization of 'Closure' is not allowed

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