【问题标题】:Laravel change input valueLaravel 更改输入值
【发布时间】:2014-05-29 05:05:36
【问题描述】:

在laravel中,我们可以通过Input::get('inputname')获取输入值。我尝试通过这样做Input::get('inputname') = "new value"; 来更改值。但随后,我收到了错误消息Can't use function return value in write context

我们是否可以更改输入值,以便以后调用Input::get('inputname') 时会得到新的修改值?

谢谢。

【问题讨论】:

  • 您必须将其分配给一个变量,然后您才能对变量执行操作。单一模式get 的函数get 接受string argument,然后对HTTP 请求执行内部操作以将数据返回给您,这就是您不能将其视为字符串的原因。然而,如果你把它的值赋给一个变量,那么这个变量就可以被如此轻松地操作了。

标签: php input laravel-4


【解决方案1】:

我使用 Raham 的答案来解决我的问题。但是,当我需要它与其他数据处于同一级别时,它将更新的数据嵌套在一个数组中。我用过:

$request->merge('someIndex' => "yourValueHere");

请注意其他 Laravel 新手,我使用 merge 方法来说明 Laravel 7 更新表单中的空复选框值。更新表单上取消选中的复选框不会返回 0,它不会在更新请求中设置任何值。因此,该值在数据库中保持不变。如果不存在任何内容,您必须检查设置值并合并新值。希望对某人有所帮助。

只是一个快速更新。如果用户没有选中一个框并且我需要在数据库中输入一个值,我会在我的控制器中执行以下操作:

    if(empty($request->input('checkbox_value'))) {
        $request->merge(['checkbox_value' => 0]);
    }

【讨论】:

    【解决方案2】:

    我也发现了这个问题,可以用下面的代码解决:

    public function(Request $request)
    {
        $request['inputname'] = 'newValue';
    }
    

    问候

    【讨论】:

      【解决方案3】:

      您可以使用Input::merge()替换单个项目。

      Input::merge(['inputname' => 'new value']);
      

      或者使用Input::replace()替换整个输入数组。

      Input::replace(['inputname' => 'new value']);
      

      这是link to the documentation

      【讨论】:

      • 注意Input:: 只是app('request') 的一个门面。所以$request->merge, ->replace... 也可以。谢谢。
      • 对于FormRequest,也可能需要将内部$request->requestParameterBag 的实例)替换为$request->request->replace(array_merge($request->request->all(), $newData));
      • 感谢您添加文档
      • $request->replace 而不是 Input::replace 从输入中删除了除我替换的值之外的所有数据。
      【解决方案4】:

      如果您的意思是要覆盖输入数据,您可以尝试这样做:

      Input::merge(array('somedata' => 'SomeNewData'));
      

      【讨论】:

      • 除非是:Input::merge(array('somedata'=> 'SomeNewData'));
      【解决方案5】:

      试试这个,它会帮助你。

      $request->merge(array('someIndex' => "yourValueHere"));
      

      【讨论】:

        【解决方案6】:

        如果您希望在 Laravel 5 中执行此操作,可以使用 Request 类中的 merge() 方法:

        class SomeController extends Controller
        {
            public function someAction( Request $request ) {
        
                // Split a bunch of email addresses
                // submitted from a textarea form input
                // into an array, and replace the input email
                // with this array, instead of the original string.
                if ( !empty( $request->input( 'emails' ) ) ) {
        
                    $emails = $request->input( 'emails' );
                    $emails = preg_replace( '/\s+/m', ',', $emails );
                    $emails = explode( ',', $emails );
        
                    // THIS IS KEY!
                    // Replacing the old input string with
                    // with an array of emails.
                    $request->merge( array( 'emails' => $emails ) );
                }
        
                // Some default validation rules.
                $rules = array();
        
                // Create validator object.
                $validator = Validator::make( $request->all(), $rules );
        
                // Validation rules for each email in the array.
                $validator->each( 'emails', ['required', 'email', 'min: 6', 'max: 254'] );
        
                if ( $validator->fails() ) {
                    return back()->withErrors($validator)->withInput();
                } else {
                    // Input validated successfully, proceed further.
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2015-02-13
          • 1970-01-01
          • 1970-01-01
          • 2021-11-30
          • 1970-01-01
          • 2019-10-19
          • 2018-04-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多