Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify Request values in laravel?

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

like image 822
Jagadesha NH Avatar asked Jul 18 '15 10:07

Jagadesha NH


People also ask

What is request () in laravel?

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.

How can you retrieve the full URL for the incoming 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.

How do I get headers in laravel?

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.

How do I create a request in laravel?

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.


1 Answers

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.

like image 178
PapaHotelPapa Avatar answered Oct 17 '22 15:10

PapaHotelPapa