Dynamically Append Model Casts with mergeCasts()

Punyapal Shah
Use mergeCasts() to dynamically append attribute casting rules to Eloquent model instances at runtime.

When working with dynamic attributes or traits on models, hardcoding all casts in $casts can be inflexible. Use mergeCasts() to add casting definitions dynamically.

use App\Models\User;

$user = new User();
$user->mergeCasts([
    'options' => 'array',
    'verified_at' => 'datetime',
]);
  • Appends cast rules to existing model casts dynamically at runtime
  • Ideal for reusable model traits that require specific attribute casts
  • Does not overwrite existing cast declarations for un-specified attributes
Tags: #Laravel #Eloquent #Casts
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips