How to modify Request values in laravel?
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.
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.