Skip to content

Default serializable_classes = false prevents caching Eloquent models without additional configuration #60714

Description

@akram-khodami

Laravel Version

13.12.0

PHP Version

8.3.31

Database Driver & Version

MySQL

Cache Driver

database

Description

With a fresh Laravel 13 installation, the default cache configuration contains:

'serializable_classes' => false,

When caching Eloquent models or Eloquent collections using the Database cache driver, the cached value cannot be restored correctly with the default configuration.

For example:

Cache::remember('products', 3600, function () {
    return Product::latest()->get();
});

$products = Cache::get('products');

dd($products);

Instead of returning an Illuminate\Database\Eloquent\Collection containing Product models, the cached value contains __PHP_Incomplete_Class instances.

As a result, attempting to access model properties results in errors such as:

Attempt to read property "name" on string

or inspecting the cached value shows __PHP_Incomplete_Class objects.

The issue disappears after explicitly allowing the serialized classes, for example:

'serializable_classes' => [
    Illuminate\Database\Eloquent\Collection::class,
    App\Models\Product::class,
]

After clearing the cache and configuration, the same code works correctly.

Expected Behavior

I expected one of the following:

  • Caching Eloquent models to work out of the box with the default configuration, or
  • The default configuration or documentation to clearly indicate that Eloquent model classes must be explicitly listed in serializable_classes.

Question

Is this the intended behavior?

If so, perhaps the default configuration or documentation could better explain that Eloquent models cannot be cached unless they are explicitly allowed.

Steps To Reproduce

  1. Create a fresh Laravel 13.12.0 application.
  2. Configure the cache driver:
CACHE_STORE=database
  1. Create a basic Product model and migration.
  2. Insert at least one product into the database.
  3. Cache an Eloquent collection:
Cache::remember('products', 3600, function () {
    return Product::latest()->get();
});
  1. Retrieve the cached value:
$products = Cache::get('products');

dd($products);
  1. Observe that the cached value contains __PHP_Incomplete_Class objects when the default configuration is:
'serializable_classes' => false,
  1. Update the configuration:
'serializable_classes' => [
    Illuminate\Database\Eloquent\Collection::class,
    App\Models\Product::class,
]
  1. Clear the cache and configuration:
php artisan optimize:clear
  1. Repeat steps 5 and 6.

The cached collection is now restored correctly as an Illuminate\Database\Eloquent\Collection containing Product model instances.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions