Can't merge request input files laravel

Viewed 560

I want merge laravel request to new value when validating passed but i cant merge or replace input key value for input files I need to change the input key value to generated unique filename and store it in storage

Here my code:

public function store(Request $request)
{
    if ($request->hasFile('photo')) {
        $photo = $request->file('photo');

        $filename = sha1(uniqid()) . '.' . $photo->getClientOriginalExtension();

        Storage::disk('photo')->put($filename, $photo->get());

        $request->merge([
            'photo' => $filename,
        ]);
    }

    return $request->all();
}

Expected result:

[
    'id'    => '60028942359046444097',
    'name'  => 'Mio Akiyama',
    'photo' => '0c7b34056cb58e812376b84279605129b22646c7.jpg',
]

Actual result:

[
    'id'    => '60028942359046444097',
    'name'  => 'Mio Akiyama',
    'photo' => [

    ],
]
1 Answers

First assign the request array into a variable.

$array = $request->all();

Then, you'll can manipulate the data of that array variable.

$array['photo'] = $filename;

return $array;
Related