Skip to content
Advertisement

Merge request not working

I am trying to replace the ‘pic’ value, which is set as an array when it comes in, with an image file name but the merge does not seem to be working.

I am not getting any errors in the log with the below code. I guess I am not allowed to embed pictures yet so there is links below. I am doing a Laravel project.

Log::info($request);

$image = $request->file('pic');
$imageName = $image->getClientOriginalName();

$request->merge(array('pic' => $imageName));
$request->file('pic')->move(base_path()."/public/profile_pics",$imageName);

Log::info($request);

Laravel Log

Any Ideas?

Advertisement

Answer

You can’t get rid of files so easily. The problem here is complex, but I will try to explain.

So basically every time You initialize request and try to get data from it, request will convert all files and store them in protected array.
Then when You request for ->all() it recursively merge all plain inputs with files inputs, so if You have plain text input with name pic and file with name pic, file have higher priority to be displayed, than plain text input.

And now dirty cheap solution:

    $request = new Request($request->all());
    $request->merge(['pic' => 'zzz']);
    dd($request->all());

Form new request with attributes from original request, then Your new request will not have any files, so You can easily merge whatever You need in it.


So in conclusion: Once You try to access ANY value from request, it will save files in protected array and there is no way to remove them now.
But if You have clean request, without any previous access to it, You can easily remove them by writing this line: $request->files = collect();

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement