I'm creating Laravel app and I'm retrieving data from DB with Eloquent ORM while responding with a JSON response. In this example I'm getting matches with some other relevant data by relationship (player1, matchRule...).
public function test() {
$match = Match::where("state", 2)
->with("player1", "player2", "points", "matchRule")->first();
return response()->json($match); // CASE A
return response()->json((object) ["id" => $match->id]); // CASE B
return response()->json((object) ["rule" => $match->match_rule]); // CASE C
}
In case A, everything's fine and all relevant data are returned. Example:
{
"id": 7,
"some_other_match_property": "something",
...
"match_rule": {
"rule_1": "something",
"rule_2": "something",
}
}
In case B, I'm getting just id of match and it also works just fine.
{
"id": 7
}
I case C, I'm trying to get property match_rule but I'm getting null. Why? As you can see, it is present in the $match object when returning the entire match in case A.
{
"rule": null
}