0

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
1
  • Have you tried json_decode($data, true); ? This will return associative arrays which is what you want. Commented Mar 30, 2020 at 11:07

3 Answers 3

1
$data;
foreach ($datas as $key => $da ) { 
   if($da->lifetime) {
    $data = (object)array(
        'lifetime' => $da->lifetime,
    );
   }
}
return response()->json($data);
Sign up to request clarification or add additional context in comments.

1 Comment

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes.
1

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.

4 Comments

I am not gonna use that info in the front that is why I need to return just certain properties of it in the back, I will try your solution and update you if it works. Thanks!
I keep getting the same message: "Trying to get property 'data' of non-objec" I have updated the main message with the json I get in case this may help. Thanks again
So you have some error in your javascript code. update the question with the relevant javascript code surrounding the console.log
that console .log it is just an example I am not using $data for anything in the fronte end
1

Try this

return \Response::json([
    "data" => [
        "lifetime"=>$data->data->lifetime
    ]
]);

1 Comment

same output... any hint why could it be?

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.