Reputation: 369
So I am connecting to call of duty api (using Laravel in the back), and getting all the info I get back in a variable ($data), then I want to return just part of it something like:
return ($data->data->lifetime)
but I get the "Trying to get property 'data' of non-object" message all the time. If I just return data, and in the front I acces to the property like this:
console.log(data.data.lifetime)
it throws me what I wants, but the thing is that I need to deal with it in the back, so which one would it be the right way to access just to the properties I want? (I have also tried using json_decode
in the return)
this is the json I am getting, I want to acces to lifetime which is inside data
{status: "success", data: {…}}
status: "success"
data:
title: "mw"
platform: "battle"
username: "fire#2749"
type: "mp"
level: 88
maxLevel: 0
levelXpRemainder: 3000
levelXpGained: 7000
prestige: 0
prestigeId: 0
maxPrestige: 0
totalXp: 960000
paragonRank: 0
paragonId: 0
s: 5
lifetime:
all: {properties: {…}}
mode: {dom: {…}, war: {…}, hq: {…}, hc_dom: {…}, hc_conf: {…}, …}
map: {}
itemData: {weapon_sniper: {…}, tacticals: {…}, lethals: {…}, weapon_lmg: {…}, weapon_launcher: {…}, …}
scorestreakData: {lethalScorestreakData: {…}, supportScorestreakData: {…}}
accoladeData: {properties: {…}}
__proto__: Object
weekly: {all: {…}, mode: {…}, map: {…}}
engagement: null
__proto__: Object
__proto__: Object
Upvotes: 0
Views: 2937
Reputation: 121
$data;
foreach ($datas as $key => $da ) {
if($da->lifetime) {
$data = (object)array(
'lifetime' => $da->lifetime,
);
}
}
return response()->json($data);
Upvotes: 1
Reputation: 6005
Try this
return \Response::json([
"data" => [
"lifetime"=>$data->data->lifetime
]
]);
Upvotes: 1
Reputation: 10264
Why do you need to return the path for that? Just access the direct value on your JavaScript:
console.log(data);
But if you NEED to return in a json format even if it's unnecessary and redundant, you need to return an array:
return response()->json([
"data" => [
"lifetime"=>$data->data->lifetime
]
]);
Note: Your controller should not return directly the data, use response()->json(...)
instead.
Upvotes: 1