php 如何修改 Laravel 中的请求值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31490224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:28:07  来源:igfitidea点击:

How to modify Request values in laravel?

phplaravel-5laravel-request

提问by Jagadesha NH

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

输出一些文本,如 /tmp/phpDPTsIn

回答by PapaHotelPapa

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

您可以merge()$request对象上使用该方法。请参阅: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.

尽管有方法名称,但它实际上替换了与由参数的键指定的成员名称相关联的任何值,而不是连接它们的值或类似的东西。

回答by Marco Pallante

You are setting the new filename using

您正在使用设置新文件名

$request->image = ...

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

但是随后您将使用Request该类的数组可访问接口来检索它。

Try to set the file name using

尝试使用设置文件名

$request['file'] = ...

or use the merge()method of the Requestclass.

或使用类的merge()方法Request