6

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?

1
  • kinda late, but as FormRequest extends Request so you can use $this->input_name Commented Feb 15, 2020 at 13:26

4 Answers 4

12

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This 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')
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').

Comments

0

Use array_get method.

$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.

Comments

-1

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

FormRequest::only

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.