ura ura
ura ura

Reputation: 61

Laravel FormRequest get input value

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?

Upvotes: 6

Views: 21336

Answers (4)

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

Upvotes: 2

madalinivascu
madalinivascu

Reputation: 32354

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.

Upvotes: 12

sumit sharma
sumit sharma

Reputation: 716

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

FormRequest::only

Upvotes: -1

Deepansh Sachdeva
Deepansh Sachdeva

Reputation: 1198

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.

Upvotes: 0

Related Questions