在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:45:18  来源:igfitidea点击:

Merge new value on Laravel array request

phplaravellaravel-5

提问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 $requestlike 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:

如果您正在寻找更优雅的东西(阅读:困难的方式)并提高代码的可重用性,您可以按照以下步骤操作:

  1. Create a Service Provider, here we register a macro to Requestobject.
  1. 创建一个服务提供者,这里我们注册一个宏到Request对象。
php artisan make:provider RegisterMergeInputRequestMacro
  1. Write your provider
  1. 写你的提供者
<?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))
            ]);
        });
    }
}
  1. Register the ServiceProviderin your config/app.phpfile:
  1. ServiceProvider在您的config/app.php文件中注册:
return [
    ...
    'providers' => [
      ...
      App\Providers\RegisterMergeInputRequestMacro::class,
      ...
    ],
    ...
];
  1. Use it like a boss:
  1. 像老板一样使用它:
$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;