search

Home  >  Q&A  >  body text

How to add data to $request in laravel?

public function store(PincardRequest $request){

}

$request can get the value submitted by the form, so how to add a value to it

The added value can be called just like the one submitted by the form

After submitting the form, add a custom value to $request for verification
PincardRequest

class PincardRequest extends Request
{

/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'yd'=>array('required','regex:/\p{Han}/u'),

    ];
}

public function messages(){
    return [
        'yd.required'=>'不能为空!',

    ];
}

}

PHP中文网PHP中文网2758 days ago512

reply all(4)I'll reply

  • 習慣沉默

    習慣沉默2017-05-16 16:50:31

    I had the same thought as you. It is to extend the request class to verify the request and supplement the request (data inside).

    But during practice, I found that Laravel is designed so that the request instance cannot be changed. Even if it is implemented forcefully, there will still be some unsolvable problems.

    So, let’s use the warehouse model to supplement data. Don't put the step of supplementing data in the request class.

    Related links:
    Is it necessary to use the Repository layer in Laravel?
    github.com/andersao/l5-repository

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 16:50:31

    $request->value=3? Are you expressing this way of adding?

    reply
    0
  • 某草草

    某草草2017-05-16 16:50:31

    // 追加一个自定义的 name=test ;value=222 的表单字段
    request()->offsetSet('test', 222);
    // 获取表单字段test的值
    $test = request()->input('test');
    echo $test; // 输出222

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 16:50:31

    For your needs, I recommend using the validate() function of the controller

    reply
    0
  • Cancelreply