How to modify request input after validation in laravel?

by raven_corwin , in category: PHP Frameworks , 6 months ago

How to modify request input after validation in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , 6 months ago

@raven_corwin 

In Laravel, if you want to modify the request input after validation, you can use the merge() method available on the request object.


Here's how you can do it:

  1. After validating the request using a validation rule or custom validation logic, you can retrieve the input values using the validated() method on the request object. This method returns the validated input values:
1
$validatedData = $request->validated();


  1. You can then modify the validated data as per your requirement.
  2. To modify the request input, you can use the merge() method on the request object. This method allows you to merge additional input values into the request's input data:
1
$request->merge(['key' => 'value']);


  1. You can now access the modified input using the usual syntax, for example:
1
$value = $request->input('key');


By following these steps, you should be able to modify the request input after validation in Laravel.