Laravel FormRequest 获取输入值

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

Laravel FormRequest get input value

phplaravellaravel-5

提问by ura ura

I try to use FormRequest:

我尝试使用 FormRequest:

class RegistrationForm extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name'=>'required',
            'email'=>'required|email',
            'password'=>'required|confirmed'
        ];
    }

    public  function persist(){
        $user=new User();
        $user->name=$this->only(['name']);
        $user->email=$this->only(['email']);
        dd($this->only(['password']);
        auth()->login($user);
    }
}

I need get in persist() method inputs value from my requst. I tried to get 'password' value, but I got array. How can I get input value like a string?

我需要从我的请求中获取 persist() 方法输入值。我试图获得“密码”值,但我得到了数组。如何像字符串一样获取输入值?

回答by madalinivascu

You can get the values using the input()function:

您可以使用以下input()函数获取值:

public function persist() {
    $user = new User();
    $user->name = $this->input('name');
    $user->email = $this->input('email');
    dd($this->input('password'));
    auth()->login($user);
}

Ps: I suggest you do your logic in the controller not in the request class.

Ps:我建议你在控制器中而不是在请求类中做你的逻辑。

回答by Deepansh Sachdeva

Use array_getmethod.

使用array_get方法。

$value = array_get($your_array, 'key_name');

PS: array_getaccepts a third argument, which is returned when given key is not found in the give array.

PSarray_get接受第三个参数,当在给定数组中找不到给定键时返回该参数。

回答by sumit sharma

According to documentation FormRequest::only will return array type data. You need to extract value from that array.

根据文档 FormRequest::only 将返回数组类型数据。您需要从该数组中提取值。

FormRequest::only

表单请求::仅