Laravel FormRequest get input value
13,924
Solution 1
You can get the values using the input()
function:
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.
Solution 2
Form request classes extend the Request class, so you can refer to the current request (and any methods) using $this, i.e. $this->input('password').
Author by
ura ura
Updated on June 12, 2022Comments
-
ura ura almost 2 years
I try to use 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?
-
ctf0 about 4 yearskinda late, but as
FormRequest extends Request
so you can use$this->input_name
-
-
Daniel Sá about 2 yearsThis is useful when we need ignore a unique rule on update one database data.. Like this:
Rule::unique('tab')->ignore($this->input('id_field'), 'id_field')