2

I have an issue for Request Laravel when i'm uploading file with key 'siup', the Request data shown like this:

"_token" => "Ab9zfuQn0rb0exCx7IdMcnAxQWi4iqWcfcDy319B"
"_method" => "PUT"
"first_name" => "first"
"last_name" => "aaa"
"email" => "[email protected]"
"province" => "11"
"city_id" => "38"
"address" => "asdasd"
"phone" => "1234567890"
"company_type" => "koperasi"
"company_name" => "qqq"
"company_address" => "qqq"
"pic" => "qqqa"
"position" => "qqq"
"siup" => UploadedFile {#30 ▶}

i want to do this to the request response

$request->merge(['siup'=>$myVar]);

but the key siup did not change. i want to change the siup value to insert it to database with laravel eloquent update.

1 Answer 1

2

The request data exposed by the Request object comes from two different sources: the query data and the files. When you dump the contents of the request data, it merges these two sources together and that is your output.

When you use the merge(), replace(), etc. methods, it is only manipulating the query data. Therefore, even though you're attempting to overwrite the siup data, you're actually only changing the siup key in the query data. The siup key in the files data is not touched. When you dump the contents of the request data again, the siup files data overwrites your siup query data.

You will save yourself a lot of trouble if you just get your data as an array, and then just use the array as needed. This is a lot safer and easier than trying to manipulate the Request object, and is probably a lot more along the lines of what you should be doing anyway.

Something like:

$data = $request->except('siup');
$data['siup'] = $myVar;

// now use your data array
MyModel::create($data);
Sign up to request clarification or add additional context in comments.

3 Comments

so i can't remove siup key on my request variable?
@patricus THANK YOU !!! I spent godamn hours trying to figure out why the replace on my uploaded file didn't work, now i'm doing a dirty thing by replacing request files with $request->files->replace(array('file1'->file1)), but it works ! (in my case it's a single file upload every time)
Somehow $this->validate($request, $rules) has an unchanged $request for the file part again...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.