21

I have the following code,

my question is how to modify Request values?

public function store(CategoryRequest $request)
{
    try {
        $request['slug'] = str_slug($request['name'], '_');
        if ($request->file('image')->isValid()) {
            $file = $request->file('image');
            $destinationPath = public_path('images/category_images');
            $fileName = str_random('16') . '.' . $file->getClientOriginalExtension();
            $request->image = $fileName;
            echo $request['image'];
            $file->move($destinationPath, $fileName);
            Category::create($request->all());
            return redirect('category');
        }
    } catch (FileException $exception) {
        throw $exception;
    }
}

But,

on each request the output of

echo $request['image'];

outputs some text like /tmp/phpDPTsIn

cweiske
  • 30,033
  • 14
  • 133
  • 194
Jagadesha NH
  • 2,529
  • 2
  • 23
  • 41

2 Answers2

32

You can use the merge() method on the $request object. See: https://laravel.com/api/5.2/Illuminate/Http/Request.html#method_merge

In your code, that would look like:

public function store(CategoryRequest $request)
{
    try {
        $request['slug'] = str_slug($request['name'], '_');
        if ($request->file('image')->isValid()) {
            $file = $request->file('image');
            $destinationPath = public_path('images/category_images');
            $fileName = str_random('16') . '.' . $file->getClientOriginalExtension();
            $request->merge([ 'image' => $fileName ]);
            echo $request['image'];
            $file->move($destinationPath, $fileName);
            Category::create($request->all());
            return redirect('category');
        }
    } catch (FileException $exception) {
        throw $exception;
    }
}

In spite of the methods name, it actually replaces any values associated with the member names specified by the keys of the parameter rather than concatenating their values or anything like that.

cweiske
  • 30,033
  • 14
  • 133
  • 194
PapaHotelPapa
  • 687
  • 5
  • 19
  • i dont know why questioner accepted the answer because in laravel it is not possible to overwrite `File` type variabel in `$request` because `$request->all()` method replaces keys of normal input with file type if both are same, e.g. `$request['image']` is bound to replaced by `$request->file('image')`. – Haritsinh Gohil Jun 27 '19 at 12:09
11

You are setting the new filename using

$request->image = ...

but then you are retreiving it using the array accessible interface of the Request class.

Try to set the file name using

$request['file'] = ...

or use the merge() method of the Request class.

Marco Pallante
  • 3,923
  • 1
  • 21
  • 26
  • I tried even $request['image'] = $fileName;but doesn't work – Jagadesha NH Jul 18 '15 at 13:39
  • 2
    Do you really need to change the `$request` variable? Why don't you get the input values into another variable and then change that? – Marco Pallante Jul 18 '15 at 13:48
  • 2
    One reason a person might want to change a value in the `$request` object is that, if they're creating a new resource, they will need to break out and save each attribute separately (by passing an array to the create method) just so that they can set the attribute they want to affect to the modified value. That impinges on the heritability of the code. – PapaHotelPapa Apr 07 '16 at 19:29
  • 1
    This was the correct answer for my case. Thank you! – Brugner Feb 03 '17 at 23:18