I've been banging my head against the wall for 2 days now, searching back and forth for solution for this problem, please enlighten me with this one:
I have this JavaScript Code that include a blade file and pass a data through it.
const loadTemplate = (locationinfo) => {
let info = `
<div class="location-info">
<h1>${locationinfo.business_name}</h1>
@include('pages/business-space/templates/t1',[
'locationinfo'=>'${JSON.stringify(locationinfo)}', //this is the code
])
</div>`;
return info;
}
When I log JSON.stringify(locationinfo)
in my console it is just a plain json string:
{
"id":3,
"business_name":"Wen",
"business_address":"sdfsdf",
"lat":14.764397881407836,
"lng":121.08031105807841,
"is_active":"Yes",
"created_by":null,
"date_created":"2022-06-17 11:09:42"
}
In my t1.blade.php if I echo the locationinfo variable it still displays the same:
echo $locationinfo;
//and the result:
{
"id":3,
"business_name":"Wen",
"business_address":"sdfsdf",
"lat":14.764397881407836,
"lng":121.08031105807841,
"is_active":"Yes",
"created_by":null,
"date_created":"2022-06-17 11:09:42"
}
But When I tried to decode it using json_decode
it becomes null. Here is my code:
$arr = json_decode($locationinfo); //this is null
foreach ($arr as $key => $value) {
echo $key;
}
Another Error:
$arr = json_decode($locationinfo, true);
foreach ($arr as $key => $value) {
echo $key;
}
//error: foreach() argument must be of type array|object, null given
Why is this happening? Thanks in advance.