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
Introduction. Laravel's Illuminate\Http\Request class provides an object-oriented way to interact with the current HTTP request being handled by your application as well as retrieve the input, cookies, and files that were submitted with the request.
Retrieving the Request URI The “path” method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the particular pattern specified in the argument of the method. To get the full URL, we can use the url method.
Laravel provides many details in Illuminate\Http\Request class object. You can simply get headers details using headers() method. This will return all headers in array.
Creating a request object with $myRequest = new Request(); creates the object with method = 'GET' . You can check your request's method with $myRequest->getMethod() . As the request property holds data for POST requests you cannot use $myRequest->request->add() by default.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With