拉拉维尔更改输入值

2022-08-30 09:18:20

在 laravel 中,我们可以通过 获得输入值。我尝试通过执行此操作来更改值。但是,然后,我收到错误消息说.Input::get('inputname')Input::get('inputname') = "new value";Can't use function return value in write context

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

谢谢。


答案 1

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

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

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

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

这是文档的链接


答案 2

如果你想在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.
        }
    }
}

推荐