Eloquent serialization with hidden (deprecated) attributes #45570
-
Hello all, As part of a migration strategy, I had to move an attribute (called use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
public $hidden = ['name'];
public function name(): Attribute
{
throw new \Exception('Attribute name is deprecated');
}
} So when I want to get this attribute with But when I have to serialize it (eg using From the exception stack trace, I think the problem is with this method : As my attribute Do you think it could be a bug, or that I misunderstood something ? PS : In addition, I created a Laravel example in this repository : https://github.com/rseon/laravel-deprecate-attribute |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
It seems like this behavior is intended, as the You could consider using the |
Beta Was this translation helpful? Give feedback.
It seems like this behavior is intended, as the
hidden
property only hides attributes from being included in the array representation of the model. ThetoArray
method only checks thehidden
property and does not account for custom attribute casting, which is why the exception is thrown in your case.You could consider using the
$appends
property instead of custom attribute casting to hide the deprecatedname
attribute in the serialized representation of your model.