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
Laravel FormRequest get input value
提问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_get
method.
使用array_get
方法。
$value = array_get($your_array, 'key_name');
PS: array_get
accepts a third argument, which is returned when given key is not found in the give array.
PS:array_get
接受第三个参数,当在给定数组中找不到给定键时返回该参数。
回答by sumit sharma
According to documentation FormRequest::only will return array type data. You need to extract value from that array.
根据文档 FormRequest::only 将返回数组类型数据。您需要从该数组中提取值。