Laravel 12: New Features in Request Merging & File Validation

Explore the new improvements in Laravel 12 for request data merging and file validation, making form handling and uploads more powerful and efficient.

Muhammad Ishaq
Muhammad Ishaq
19 Apr 2025
6 likes
2 minutes
Laravel 12: New Features in Request Merging & File Validation

Laravel 12 introduces refinements that enhance request handling and file validation. Two notable updates include mergeIfMissing() for request input handling and the new File::image() rule for file validation.

1. mergeIfMissing() for Request Input

Overview

The mergeIfMissing() method allows merging values into a request only if they are absent:

$request->mergeIfMissing([

    'user.last_name' => 'Muhammad',

]);

Benifts

  • Ensures a default value is added only if the field is empty.

  • Prevents accidental overwrites of user-submitted data.

  • Useful for setting default values in APIs and forms.

2. File Validation with File::image()

Overview

Laravel 12 introduces a class-based validation rule for images, making the validation process cleaner and more expressive:

use Illuminate\Validation\Rules\File;

 'photo' => ['required', File::image(allowSvg: true)],

Advantages:

  • Structured and readable syntax.

  • Explicitly allows SVG images using allowSvg: true.

  • Extensible for future enhancements.

Conclusion

Laravel 12’s new features improve developer experience by making request handling and validation more intuitive. These refinements ensure cleaner, more maintainable code while adding flexibility to common tasks.