How to add data to $request in laravel?
PHP中文网
PHP中文网 2017-05-16 16:48:31
0
4
509

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中文网

认证高级PHP讲师

reply all(4)
習慣沉默

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

刘奇

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

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

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template