Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JamieCee20's avatar

Supporting a colleague with mergeCasts issue

To the context I know, there is a base Model in a package

class ModelA extends Model {
protected $casts = [
'field'
];

In the extended model, he is trying to use the $this->mergeCasts function in the constructor to merge some casts but also override 'field'. So package field is set to text, in project he's trying to change it to 'encrypted'.

The situation is he's calling a Seeder... which is calling a factory, that factory model is calling the project level extended model, but its not obtaining the updated casts from mergeCasts().

Without the code, is anyone understanding any ideas what may be missing?

0 likes
1 reply
kevinbui's avatar

I have no idea why that fails without looking at the code of the extended model's constructor.

That might not be the right way extending casts.

In the extended model, pls use the casts function.

casting changes in the extended model will be eventually picked up:

// Illuminate\Database\Eloquent\Concerns\HasAttributes

/**
* Initialize the trait.
*
* @return void
*/
protected function initializeHasAttributes()
{
    $this->casts = $this->ensureCastsAreStringValues(
        array_merge($this->casts, $this->casts()),
    );
}

Please or to participate in this conversation.