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
- Create a fresh Laravel 13.12.0 application.
- Configure the cache driver:
- Create a basic
Product model and migration.
- Insert at least one product into the database.
- Cache an Eloquent collection:
Cache::remember('products', 3600, function () {
return Product::latest()->get();
});
- Retrieve the cached value:
$products = Cache::get('products');
dd($products);
- Observe that the cached value contains
__PHP_Incomplete_Class objects when the default configuration is:
'serializable_classes' => false,
- Update the configuration:
'serializable_classes' => [
Illuminate\Database\Eloquent\Collection::class,
App\Models\Product::class,
]
- Clear the cache and configuration:
php artisan optimize:clear
- Repeat steps 5 and 6.
The cached collection is now restored correctly as an Illuminate\Database\Eloquent\Collection containing Product model instances.
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:
Instead of returning an
Illuminate\Database\Eloquent\CollectioncontainingProductmodels, the cached value contains__PHP_Incomplete_Classinstances.As a result, attempting to access model properties results in errors such as:
or inspecting the cached value shows
__PHP_Incomplete_Classobjects.The issue disappears after explicitly allowing the serialized classes, for example:
After clearing the cache and configuration, the same code works correctly.
Expected Behavior
I expected one of the following:
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
Productmodel and migration.__PHP_Incomplete_Classobjects when the default configuration is:'serializable_classes' => false,The cached collection is now restored correctly as an
Illuminate\Database\Eloquent\CollectioncontainingProductmodel instances.