php Laravel 更改输入值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23073633/
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 16:28:13  来源:igfitidea点击:

Laravel change input value

phpinputlaravel-4

提问by user1995781

In laravel, we can get the input value via Input::get('inputname'). I try to change the value by doing this Input::get('inputname') = "new value";. But then, I get the error message saying Can't use function return value in write context.

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

Is it possible for us change the input value so that when later calling on Input::get('inputname')will get the new amended value?

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

Thanks.

谢谢。

回答by Brad

You can use Input::merge()to replace single items.

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

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

Or use Input::replace()to replace the entire input array.

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

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

Here's a link to the documentation

是文档链接

回答by Sudhir Bastakoti

If you mean you want to overwrite input data, you can try doing:

如果您的意思是要覆盖输入数据,可以尝试执行以下操作:

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

回答by Arman H

If you're looking to do this in Laravel 5, you can use the merge()method from the Requestclass:

如果你想在 Laravel 5 中做到这一点,你可以使用类中的merge()方法Request

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.
        }
    }
}

回答by Raham

Try this,it will help you.

试试这个,它会帮助你。

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

回答by anayarojo

I also found this problem, I can solve it with the following code:

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

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

Regards

问候