Skip to content

Conversation

@mahmoudmohamedramadan
Copy link
Contributor

@mahmoudmohamedramadan mahmoudmohamedramadan commented Dec 12, 2025

This PR extends the mergeHidden and mergeVisible methods to Eloquent Collections, allowing you to adjust attribute visibility across an entire collection—not just individual models.

// App\Models\User

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'last_login_at',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<string>
     */
    protected $hidden = [
        'password',
        'remember_token',
        'last_login_at',
    ];

    /**
     * The attributes that should be visible in serialization.
     *
     * @var array<string>
     */
    protected $visible = [
        'name',
        'email',
    ];
}

Runtime Usage

use App\Models\User;

$users = User::withWhereHas('comments')->get();

// Temporarily expose hidden attributes for all models in the collection
$users->mergeVisible(['last_login_at']);

// Temporarily hide attributes across the collection
$users->mergeHidden(['email']);

@taylorotwell taylorotwell merged commit a423636 into laravel:12.x Dec 12, 2025
72 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