0

I'm writing rest apis in Laravel. Everything is ok when the request parameters are not nested. I can get any input parameters by ->. e.g.

$model->update([
    'column' => $request->value,
]);

However if I'd like the parameters to have nested structure, what is the way to get them? $request->value->nested_value doesn't work as the value is not an object.

Example of nested request parameters:

{
    "parameters": {
        "name": "game9",
        "images": {
            "icon_id": 1,
            "banner_id": 1,
        }
    }
}
1
  • Please share example of your request Commented Mar 12, 2019 at 12:31

1 Answer 1

1

When you get the values from the request, they are not objects. They are arrays.

You need to access them like this:

$request->value['nested_value']

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

2 Comments

You're right but the problem is where the value is not required in the request and if the user did not set the value, an error occurs saying undefined index 'nested_value'. Is there another way to overcome such a problem?
@MohammadrezaSahelgozin You have to add a Validation the request before you use the data, laravel.com/docs/5.8/validation, this link show how you can achieve that.

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.