jyotishmoy's avatar

Accessing form values posted as objects in controller

I have a form in react using formik which stores information like name and address of an user with laravel. The address fields are stored as nested object with formik and the complete from data looks like below:

{ fname: '', mname: '', lname: '', address: { adr_line_1: '', adr_line_2: '', country: '', state: '', city: '', pincode: '' } }

Now I don't know how to access the address fields in laravel and apply validations or insert into database. I'd be glad to have suggestions regarding the approach i should follow to solve the problem. Thank You

0 likes
4 replies
filipc's avatar

For validation you can use dot notation. For example, if you wanted to check adr_line_1 then

$rules = [
	"address.adr_line_1" => "required"
]

To access them first access the root then the child

$addressLine1 = request()->address['adr_line_1']
1 like
jyotishmoy's avatar

@filipc Hi, thank you for your reply. I did tried using the dot notation. It gives the required error for the specific fields even if the data is posted.

filipc's avatar

is the address empty like in the example

jyotishmoy's avatar

@filipc Its not. I'm posting values from the form. If I do print_r its gives the following

Array ( [fname] => John [mname] => '' [lname] => Doe [address] => [object Object] )

Please or to participate in this conversation.