Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 338
roerjo Avatar asked May 14 '16 22:05

roerjo


People also ask

Why is it pull request and not merge request?

The name “pull request” comes from the idea that you're requesting the project to “pull” changes from your fork. You initiate a pull request when you're ready to begin merging new changes in the code to the project's main repository. You're informing the rest of the project team about your intentions.


1 Answers

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();

like image 199
Giedrius Kiršys Avatar answered Sep 21 '22 00:09

Giedrius Kiršys