Laravel change input value

后端 未结 6 1613
灰色年华
灰色年华 2020-12-01 05:45

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\";

6条回答
  •  时光取名叫无心
    2020-12-01 06:35

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

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

提交回复
热议问题