Skip to content

Add new mergeVisible, mergeHidden and mergeAppends methods. #56678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 18, 2025

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