Skip to content

Conversation

@jonerickson
Copy link
Contributor

This PR adds additional attribute helper methods to Eloquent models for merging visibility-related arrays, bringing them in line with existing helpers like mergeCasts, mergeFillable, and mergeGuarded.

These new methods simplify scenarios where traits need to augment a model’s configuration without overwriting existing values.

Example

Today, when adding 2FA support via a trait, you might write:

Trait: HasTwoFactorAuthentication.php

protected function initializeTwoFactorAuthentication(): void
{
    $this->setHidden(array_merge($this->getHidden(), [
        'app_authentication_secret',
        'app_authentication_recovery_codes',
    ]));

    $this->mergeCasts([
        'app_authentication_secret' => 'encrypted',
        'app_authentication_recovery_codes' => 'encrypted:array',
    ]);
}

With this PR, the hidden attributes can be merged just as cleanly as casts:

protected function initializeTwoFactorAuthentication(): void
{
    $this->mergeHidden([
        'app_authentication_secret',
        'app_authentication_recovery_codes',
    ]);

    $this->mergeCasts([
        'app_authentication_secret' => 'encrypted',
        'app_authentication_recovery_codes' => 'encrypted:array',
    ]);
}

Existing Methods

  • $model->mergeCasts()
  • $model->mergeFillable()
  • $model->mergeGuarded()

New Methods Added

  • $model->mergeVisible()
  • $model->mergeHidden()
  • $model->mergeAppends()

@taylorotwell taylorotwell merged commit 5073f78 into laravel:12.x Aug 18, 2025
60 checks passed
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.

2 participants