The issue is likely due to the way you are defining your casts in the model. In Laravel, the casts property should be a protected property, not a method. The protected function casts(): array syntax is only supported in Laravel 9.21 and above (see Laravel release notes). If your public site is running an older Laravel version, it will ignore the method and look for the property instead.
How to fix:
Replace your casts() method with a protected $casts property in your model:
<?php
namespace App\Models;
use Illuminate\Support\Str;
use App\Casts\SecretEncrypt;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Secrets extends Model
{
use HasFactory;
protected $keyType = 'string';
protected $casts = [
'secret' => SecretEncrypt::class,
'passphrase' => SecretEncrypt::class,
];
}
Why does this work?
- Laravel versions prior to 9.21 only support the
$castsproperty. - If you use the
casts()method on an older version, it will be ignored, resulting in an empty#castsarray.
Next steps:
- Update your model as shown above.
- Clear your config and cache again:
php artisan config:clear php artisan cache:clear - Test retrieving your model again.
If you want to use the casts() method, make sure your Laravel version is 9.21 or above.
Summary:
On your public site, use the $casts property instead of the casts() method unless you're sure you're running Laravel 9.21+. This should resolve the issue of Eloquent ignoring your casts.