laravel 如何在laravel中更改请求参数的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36812476/
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
How to change value of a request parameter in laravel
提问by Morteza Negahi
I need to change value of my request parameter like this:
我需要像这样更改我的请求参数的值:
$request->name = "My Value!";
I use this code but does not work:
我使用此代码但不起作用:
$request->offsetSet('img', $img);
回答by Alexey Mezenin
Try to:
尝试:
$requestData = $request->all();
$requestData['img'] = $img;
Another way to do it:
另一种方法:
$request->merge(['img' => $img]);
Thanks to @JoelHinz for this.
感谢@JoelHinz 为此。
If you want to add or overwrite nested data:
如果要添加或覆盖嵌套数据:
$data['some']['thing'] = 'value';
$request->merge($data);
If you do not inject Request $request
object, you can use the global request()
helper or \Request::
facade instead of $request
如果不注入Request $request
对象,则可以使用全局request()
助手或\Request::
外观代替$request
回答by doncadavona
Use merge()
:
使用merge()
:
$request->merge([
'user_id' => $modified_user_id_here,
]);
Simple! No need to transfer the entire $request->all()
to another variable.
简单的!无需将整个转移$request->all()
到另一个变量。
回答by Alaa Moneam
If you need to customize the request
如果您需要自定义请求
$data = $request->all();
you can pass the name of the field and the value
您可以传递字段的名称和值
$data['product_ref_code'] = 1650;
and finally pass the new request
最后通过新的请求
$last = Product::create($data);
回答by Phuc
It work for me
它对我有用
$request = new Request();
$request->headers->set('content-type', 'application/json');
$request->initialize(['yourParam' => 2]);
check output
检查输出
$queryParams = $request->query();
dd($queryParams['yourParam']); // 2
回答by Oleg
If you use custom requests for validation, for replace data for validation, or to set default data (for checkboxes or other) use override method prepareForValidation()
.
如果您使用自定义请求进行验证、替换验证数据或设置默认数据(用于复选框或其他),请使用 override method prepareForValidation()
。
namespace App\Http\Requests\Admin\Category;
class CategoryRequest extends AbstractRequest
{
protected function prepareForValidation()
{
if ( ! $this->get('url')) {
$this->merge([
'url' => $this->get('name'),
]);
}
$this->merge([
'url' => Str::slug($this->get('url')),
'active' => (int)$this->get('active'),
]);
}
}
I hope this information will be useful to somebody.
我希望这些信息对某人有用。
回答by phoenix
Great answers here but I needed to replace a value in a JSON request. After a little digging into the code, I came up with the following code. Let me know if I'm doing something dumb.
这里的答案很好,但我需要替换 JSON 请求中的值。在深入研究代码后,我想出了以下代码。如果我在做一些愚蠢的事情,请告诉我。
$json = $request->json()->all();
$json['field'] = 'new value';
$request->json()->replace($json);
回答by Ja Ouad
If you need to update a property in the request, I recommend you to use the replace method from Request class used by Laravel
如果您需要更新请求中的属性,我建议您使用 Laravel 使用的 Request 类中的 replace 方法
$request->replace(['property to update' => $newValue]);