0

I am trying to retrieve some part of request() in my Form Request class named StoreApplicantLanguage.php. The request key called 'languages' and it has an array of objects containing a key-value pair to be stored in my `applicant_languages' table.

Here is my JSON request from Postman:

{
    "languages": [
        {
            "language": "English",
            "capability": 1
        }
    ]
}

Looks normal right?! But, when I'm trying to get the values of the languages key like this:

$requestLanguages = request()->languages;
dd($requestLanguages);

, it shows null. I tried to restart my server, do php artisan config:cache, but none are works. But when I change the key name in the request object to language, it works!

Also, the request object has another named field like families, and I can get the values inside by doing request()->families.

I have no idea at all how this can be happen. Anyone can explain my case, please!

Thanks in advance!

Edit: From Malkhazi Dartsmelidze's answer I realized that I misstyped the question. I didn't write comma after '1' value in my JSON request

1 Answer 1

1

It works fine on my system. Maybe you that's because you are passing invalid json.

{
    "languages": [
        {
            "language": "English",
            "capability": 1
        }
    ]
}

Try passing this JSON (I deleted last comma after '1')

Also note that Request is object and there is properties that are used already and $request variable can return it. You can use $request->get('languages') to get parameter from request

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

1 Comment

Thanks for your answer, but I am not working in a Controller or method that using $request variable as an instance of Request as you said. But from your answer, now I can get the data with languages key by doing $families = Request::get('families'); Thanks a lot, sorry for my ignorance.

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.