7

I am trying to submit my form input as json format. After decoding json data into an array I can't access array.

{
    "info": {
        "q_title": "hello",
        "q_description": "ddd",
        "q_visibility": "1",
        "start_date": "Thu, 05 Oct 2017 06:11:00 GMT"
    }
}

This is my json data. My Laravel controller is following:

public function store_quiz(Request $request)
    {
        $data = json_decode($request->getContent(), true);

        $input = $arrayName = array(
            'title' => $data["info"]["q_title"], 
        );

        CreateQuiz::create($input);

        $redirect = '/';
        return $redirect;
    }

Unfortunately $data["info"]["q_title"] returns NULL. How to access "q_tittle" of my json??

4
  • can you print_r()$request->getContent());.may be that is null Commented Oct 5, 2017 at 6:42
  • show us $data, so we can see your array Commented Oct 5, 2017 at 6:43
  • Your request body isnt a valid json Commented Oct 5, 2017 at 6:43
  • what $request->getContent() returns. Is it JSON encoded string? Moreover are you sure your JSON encoded string always has the above attributes which you are trying to access ? Commented Oct 5, 2017 at 6:52

4 Answers 4

5

just access your data after json_decode like this without a second argument.

$data->info->q_title

and by using the second argument as true, which will convert your object into an array.

$data = json_decode($request->getContents(),true) //then use
$data['info']['q_title']

and if you are not getting anything in $request->getContents() then the problem lies in the request that you are sending.

Also, check this two links for how to retrieve JSON payload

Laravel 5: Retrieve JSON array from $request

Posting JSON To Laravel

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

5 Comments

don't know why I m getting -2 in this.
@Dhaval.he given true so When TRUE, returned objects will be converted into associative arrays.that may be the reason down voted
@DhavalPurohit take a look into documentation php.net/manual/en/function.json-decode.php (-;
ok then the problem should lie in $request->getContent() @iCoders
not sure .so $data output only we can decide.without that i cant comment any think without looking code and outptu
1

Maybe $request->getContent() content is different from your example, because for me it works.

Try to check what you have in $request->getContent().

Example:

<?php
$source = '{
    "info": {
        "q_title": "hello",
        "q_description": "ddd",
        "q_visibility": "1",
        "start_date": "Thu, 05 Oct 2017 06:11:00 GMT"
    }
}';
$data = json_decode($source, true);
echo $data['info']['q_title'];

1 Comment

Yes. Actually problem lies in $request->getContent(). @Neodan
0

Solution:

My data was saved in var data in jQuery.

To post data I used

$.ajax({  .... data: {data: data} .... });

instead of

 $.ajax({  .... data: data .... }); 

So I got unexpected value from $request->getContent().

Comments

-1

Here's a more Laravel style method of doing this:

public function store_quiz(Request $request)
{
    $input = $arrayName = array(
        'title' => $request->input('info.q_title'), 
    );

    CreateQuiz::create($input);

    $redirect = '/';
    return $redirect;
}

And ensure your javascript app sets the Content-Type header to application/json. Laravel will json_decode() it for you.


Source: https://laravel.com/docs/5.5/requests (Retrieving JSON Input Values)

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.