Merge a Laravel Request on the fly

How to change or add a new value to a Laravel request parameter

The solution is in Part B.

Part A: ( Introduction )

Let’s first see how merge() helper function works in Laravel:

$collection = collect(['product_id' => 1, 'price' => 100]);
$merged = $collection->merge(['price' => 200, 'discount' => false]);
$merged->all();

// ['product_id' => 1, 'price' => 200, 'discount' => false]
$collection = collect(['Desk', 'Chair']);

$merged = $collection->merge(['Bookcase', 'Door']);

$merged->all();

// ['Desk', 'Chair', 'Bookcase', 'Door']

Part B:

B-I: What we are merging is a simple value.

https://panjeh.medium.com/check-variable-is-a-laravel-collection-4757c005463d
$fileName = “IMAGE_1”

B-II-adding:

In the case of adding and not updating, just need to follow B-I.

B-II-updating:

If 'user' key and its value (that is an array $arrayOfUserData) is in$request->all()and we want to modify that array $arrayOfUserData, first, we have to modify the array somewhere and then merge the modified array with the current request.

Posting daily about Python, Laravel, Livewire, Nuclear Physicist PhD

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store