在 Laravel 数组请求上合并新值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37131284/
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
Merge new value on Laravel array request
提问by Sebastian Corneliu V?rlan
If I have all my input values on $request->input('users')
how can I merge new one?
如果我有关于$request->input('users')
如何合并新的所有输入值?
Something like this:
像这样的东西:
$request->merge(['users.dob' => '2000']);
But obvious is not working.
但显然是行不通的。
回答by Okiemute Omuta
First, we need to get all items that belong in that array and save temporarily.
首先,我们需要获取属于该数组的所有项目并临时保存。
For instance,
例如,
$users = $request->users;
This way, you can access a user's data like $users['name']
or $users['surname']
.
这样,您就可以访问用户的数据,例如$users['name']
或$users['surname']
。
Next, update or add whatever data you need to. For instance, like this:
接下来,更新或添加您需要的任何数据。例如,像这样:
$users['dob'] = "2000";
$users['name'] = ucwords($request->users['name']);
...
When we are done, we merge our temporary array back into $request
like this:
完成后,我们将临时数组合并回$request
如下所示:
$request->merge(['users' => $users]);
回答by Alexey Mezenin
Try this:
尝试这个:
$request->request->add(['users.dob', '2000']);
It seems merge()
doesn't work anymore.
好像merge()
已经不行了。
回答by krisanalfa
Try this:
尝试这个:
$originalInput = $request->input('users');
$defaultValue = [
'dob' => 2000
];
$mergedResult = array_merge($defaultValue, $originalInput);
$request->merge([
'users' => $mergedResult
]);
If you're looking for something more elegant (read: hard way) and improve the reusability of your code, you can follow this steps:
如果您正在寻找更优雅的东西(阅读:困难的方式)并提高代码的可重用性,您可以按照以下步骤操作:
- Create a Service Provider, here we register a macro to
Request
object.
- 创建一个服务提供者,这里我们注册一个宏到
Request
对象。
php artisan make:provider RegisterMergeInputRequestMacro
- Write your provider
- 写你的提供者
<?php
namespace App\Providers;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
class RegisterMergeInputRequestMacro extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
Request::macro('mergeInput', function (string $name, array $defaultValue) {
$this->merge([
$name => array_merge($defaultValue, $this->input($name))
]);
});
}
}
- Register the
ServiceProvider
in yourconfig/app.php
file:
ServiceProvider
在您的config/app.php
文件中注册:
return [
...
'providers' => [
...
App\Providers\RegisterMergeInputRequestMacro::class,
...
],
...
];
- Use it like a boss:
- 像老板一样使用它:
$request->mergeInput('users', [
'dob' => 2000
]);
回答by rc.adhikari
Try this:
尝试这个:
$request->request->add([
'new-field1' => 'value 1',
'new-field2' => 'value 2'
]);
回答by D Coder
You can try this for
你可以试试这个
public function __construct(\Illuminate\Http\Request $request)
{
$request->request->add(
['date_of_birth' => implode('-',$request->only('year', 'month', 'day'))]);
}
回答by Rahman Qaiser
Try this:
尝试这个:
$data= $request->input('users');
$data['dob'] = 2000;