App-owned PATCH input in Laravel PHP
Scope
Inline one flat nullable-string PATCH data object using an explicit application boundary. Preserve missing/null/string semantics, normalization, rules, guards and output fields. Exclude nested data, rule inference, custom casts, lazy includes, dynamic creation, collection wrapping and TypeScript generation. Keep excluded callers on the package-backed path and preserve its MIT notice for adapted code. Check get-recipe's current port catalog when considering alternatives. This guidance has not been exercised in your app; verify it with the checks below.
Source behaviour
src/Concerns/BaseData.php builds a pipeline including authorization, mapping, injection, validation, defaults and casts. src/DataPipes/ValidatePropertiesDataPipe.php follows a creation strategy; the inspected config/data.php defaults to requests-only validation. validateAndCreate in src/Concerns/ValidateableData.php explicitly requests validation. Replacing a DTO constructor alone is not a rules or policy replacement.
src/DataPipes/DefaultValuesDataPipe.php distinguishes key presence before applying configured defaults, optional values or nullable fallback. src/Resolvers/TransformedDataResolver.php drops Optional values but preserves null and can map output names. Creation/transformation tests exercise those differences. Using isset or null coalescing to detect a PATCH field would merge missing and null.
Implementation
- Inventory the chosen class's field mapping, defaults, rules, normalizers, authorization, transforms and every HTTP/direct/job caller. Capture original request/error/resource fixtures, including trimming and empty-to-null middleware. Select one flat field; preserve excluded package behavior until separately replaced.
- Keep request-size/content-type/JSON error handling in the existing HTTP boundary. Preserve the distinction between JSON object and list, even for
{}versus[]; decoding everything to associative arrays loses that empty-shape distinction. Handle malformed JSON with exceptions and test the chosen duplicate-key policy. Do not decode request input twice. - Use a presence flag and nullable string after object/type validation. Apply the endpoint's existing rules and normalization consistently for jobs/commands as well as HTTP. The example deliberately rejects unknown keys and non-string scalars rather than reproducing all package cast/default configurations. It preserves strings verbatim; don't mistake it for a complete Form Request.
- Authorize trusted actor, tenant, resource and editable field separately. Apply only present allowlisted values, not the whole input array through mass assignment. Retain the existing optimistic-version/locking policy. For related writes, use the correct connection's transaction, propagate exceptions, and throw if an Eloquent save is vetoed with false so partial work does not commit.
- Return an explicit resource projection, preserving output naming, envelope and omitted/null fields. Do not serialize the entire model or authorize fields merely because a DTO exposes them. Preserve required post-commit effects and scoped cache invalidation. No migration, global DTO registry or extra cache is needed for parsing.
Complete pure field classifier for a JSON object decoded as \stdClass, not an associative array. It checks keys/types and returns explicit presence plus nullable value; normalization, domain rules and authorization remain outside it. Fully qualified class names work inside an application namespace:
function namePatch(\stdClass $input): array
{
$fields = get_object_vars($input);
foreach ($fields as $key => $value) {
if ($key !== 'display_name') {
throw new \InvalidArgumentException('unknown field');
}
}
$present = array_key_exists('display_name', $fields);
$value = $present ? $fields['display_name'] : null;
if ($value !== null && !is_string($value)) {
throw new \InvalidArgumentException('display_name must be string or null');
}
return ['present' => $present, 'value' => $value];
}
Use a method/name beside the existing input/action, not a new global helper solely to copy the example.
Acceptance checklist
- From stored
"Ada",{}leaves it unchanged, null explicitly clears it and"0"assigns that string. The classifier preserves empty string and" Åda "; test the endpoint's actual middleware/rules separately. Reject numeric, boolean, array/object values and unknownis_admininput. - Malformed JSON, top-level null/arrays and oversized requests fail through the real boundary. Preserve mapped names, duplicate-key policy, validation status/error keys and response omission/null behavior.
- HTTP, direct and queued use enforce required rules and guards. Unauthorized/cross-tenant input cannot select actor/tenant or change stored values/caches.
- Omitted fields never overwrite records. Exercise existing stale-write rejection, observer-vetoed saves, rollback and post-commit failure without replaying committed work. Private fields never appear in output.
- Compare source-backed defaults/optional/null/transformation fixtures; search all data classes/helpers and
composer why spatie/laravel-databefore removing the package.
Run the application's request/resource/job/persistence tests on its actual framework/database, recording versions, decisive results and deliberate differences. These criteria still require destination-app execution.
Sources
- MIT reference source:
src/Concerns/BaseData.php,src/Concerns/ValidateableData.php,src/DataPipes/ValidatePropertiesDataPipe.php,src/DataPipes/DefaultValuesDataPipe.php,src/Resolvers/TransformedDataResolver.php,config/data.php,tests/CreationTest.php, andtests/TransformationTest.php. PreserveLICENSE.md. - Framework source checked:
src/Illuminate/Database/Eloquent/Model.phpandsrc/Illuminate/Database/Concerns/ManagesTransactions.php; reconcile metadata with the app's lockfile and actual request normalization. - Port options: check
get-recipe's current port catalog (sourced from Package Port) at implementation time.