Skip to content

[12.x] Allow mass assignment for value object casting.#56871

Merged
taylorotwell merged 1 commit into
laravel:12.xfrom
AbdelElrafa:12.x
Sep 2, 2025
Merged

[12.x] Allow mass assignment for value object casting.#56871
taylorotwell merged 1 commit into
laravel:12.xfrom
AbdelElrafa:12.x

Conversation

@AbdelElrafa

Copy link
Copy Markdown
Contributor

Hey Taylor,

I ran into an issue this morning when trying to mass assign an attribute that's a cast that wasn't being guarded.

Given a model that uses a value cast object, that's not explicitly guarded:

class Person extends Model
{
    protected $guarded = ['id'];
    public $timestamps = false;
    protected $casts = [
        'address' => AsAddress::class,
    ];
}

I would expect to be able to mass assign the attribute:

Person::create(['address' => new AddressDto('123 Main St.', 'Anytown, USA')]);

However, this was throwing: Illuminate\Database\Eloquent\MassAssignmentException: Add [address] to fillable property to allow mass assignment on [Illuminate\Tests\Database\Person].

I tried to use the Model::handleDiscardedAttributeViolationUsing(...) method added in #44664 to handle that within my app, but wasn't able to since the value that was trying to be set wasn't being passed into the closure. That's probably another update needed to make that hook more useful.

After digging into how we determine if an attribute is isFillable() or not, I see that we're already checking if the attribute hasSetMutator or hasAttributeSetMutator so it felt natural to also check if the attribute isClassCastable.

@taylorotwell taylorotwell merged commit 149a1d1 into laravel:12.x Sep 2, 2025
65 checks passed
@ghaskell

ghaskell commented Sep 2, 2025

Copy link
Copy Markdown

🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants