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??
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With