Decode json data for an array and access the array in PHP (Laravel)

I am trying to present my form input as json format. After decoding the json data into an array, I cannot access the 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. Laravel Controller:

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 the "q_tittle" of my json ??

+4
source share
4 answers

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

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
source

just access your data after json_decodelike this without a second argument.

$data->info->q_title

true, .

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

$request->getContents(), , .

, , JSON

Laravel 5: JSON $request

JSON To Laravel

0

:

var data jQuery.

,

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

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

, $request->getContent().

0

Laravel:

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

    CreateQuiz::create($input);

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

, javascript Content-Type application/json. Laravel json_decode() .


: https://laravel.com/docs/5.5/requests ( JSON)

0

Source: https://habr.com/ru/post/1686939/


All Articles