To leverage the new DtoUtility::updateEntityList() feature in v2.3.3, start by importing the DtoUtility class (likely from the package namespace, e.g., Vendor\Package\DtoUtility). The key change is the new optional parameter for property-based comparison when updating entity lists.
First use case:
use Vendor\Package\DtoUtility;
// Example: Update a collection of User entities, comparing only 'name' and 'email'
$updatedEntities = DtoUtility::updateEntityList(
$originalEntities, // Collection of Eloquent models or arrays
$dtoList, // Data Transfer Objects (DTOs) with updated data
['name', 'email'] // NEW: Specify properties for comparison
);
Check the package’s README.md for:
DtoUtility.$originalEntities must be a collection or can be an array.Use the new parameter to optimize performance when only specific fields need validation before updating. For example:
// Update only 'status' and 'last_updated_at' fields
$updatedUsers = DtoUtility::updateEntityList(
$users,
$userDtos,
['status', 'last_updated_at']
);
If working with Eloquent models, ensure the compared properties exist in the model’s $fillable or $guarded arrays to avoid mass assignment errors. Example:
// In User model:
protected $fillable = ['name', 'email', 'status'];
// Safe update:
$updated = DtoUtility::updateEntityList($users, $dtos, ['name', 'status']);
Pair with Laravel’s validation to enforce rules before updating:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($dtoList, [
'*.name' => 'required|string|max:255',
'*.email' => 'required|email',
]);
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()], 422);
}
$updated = DtoUtility::updateEntityList($users, $dtoList, ['name', 'email']);
Fetch properties dynamically (e.g., from a config or request):
$compareFields = config('app.entity_comparison_fields'); // ['id', 'slug']
$updated = DtoUtility::updateEntityList($entities, $dtos, $compareFields);
Property Existence:
DtoUtility::updateEntityList($users, $dtos, ['nonexistent_field']); // Behavior?
Case Sensitivity:
camelCase vs snake_case). Use array_map('strtolower', ...) if needed.Performance:
DTO Structure:
['name', 'email'], the DTO must have these fields populated.\Log::debug('Comparison properties:', ['properties' => ['name', 'email']]);
null values in compared fields.Custom Comparison Logic:
DtoUtility or using a wrapper:
class CustomDtoUtility extends DtoUtility {
public static function updateEntityList($entities, $dtos, $properties = []) {
// Add custom logic (e.g., ignore null values)
return parent::updateEntityList($entities, $dtos, $properties);
}
}
Event Hooks:
eloquent.updating events to add pre-update logic:
User::updating(function ($user) {
// Modify $user->attributes before DtoUtility updates it
});
Configuration:
config/package.php:
'default_comparison_fields' => ['id', 'slug'],
$fields = config('package.default_comparison_fields');
$updated = DtoUtility::updateEntityList($entities, $dtos, $fields);
How can I help you explore Laravel packages today?