Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rajeshtva's avatar

How to get neat and clean json response

i have a model relation set up like User has Tracks(another model). so when return $user = User::first() then details of user comes but i want to load user tracks too. i wrote $tracks = $user->tracks. This gives all tracks that user has in a separate variable. but when i return $user. it also loads tracks in $user. but i want tracks from $user removed from json Response.

How can i do it?

0 likes
7 replies
Tray2's avatar

I would do

$user = User::first()->with('tracks');

And not do it twice like you are doing.

1 like
rajeshtva's avatar

@tray2 sir i am not doing it twice. first i am fetching only user details in one variable. then i am fetching tracks in another variable. but in json response tracks is coming attached with user although tracks already has tracks. so i don't want user variable to have tracks.

Tray2's avatar

Show me the controller code and the view code.

rajeshtva's avatar
    public function get_user_details(Request $request, User $user)
    {
        $tracks = $user->tracks;
        $data['user'] = $user;
        

        foreach($tracks as $t){
            $t->spent_time = $t->durations()->where('user_id', $user->id)->get();
        }

        $data['tracks'] = $tracks;

        return send_response(200, $data, 'success', 200);
    }

@tray2 sir this is the code i wrote and this is the response i am getting. you can clearly see that tracks is being repeated at both places. send_response() is just a wrapper for this response.

{
    "message": "success",
    "status": 200,
    "data": {
        "user": {
            "id": 1,
            "name": "Johny Doe Sr.",
            "username": "user1",
            "email": "[email protected]",
            "status": "1",
            "registered_through": "self",
            "email_verified_at": "2021-01-04T09:49:49.000000Z",
            "created_at": "2021-01-04T09:49:49.000000Z",
            "updated_at": "2021-01-04T09:49:49.000000Z",
            "email_verified": "1",
            "email_verification_token": null,
            "tracks": [
                {
                    "id": 1,
                    "name": "track 1",
                    "starting_lat": null,
                    "starting_lng": null,
                    "from": "Guwahati",
                    "ending_lat": null,
                    "ending_lng": null,
                    "to": "Banglore",
                    "distance": "1300",
                    "kml_file": null,
                    "status": "0",
                    "created_at": "2021-01-04T09:49:47.000000Z",
                    "updated_at": "2021-01-04T09:49:47.000000Z",
                    "spent_time": [
                        {
                            "id": 9,
                            "start_time": "2012-07-17T09:21:49.000000Z",
                            "end_time": "2012-07-17T19:57:48.000000Z",
                            "user_id": "1",
                            "track_id": "1",
                            "created_at": "2021-01-04T09:49:55.000000Z",
                            "updated_at": "2021-01-04T09:49:55.000000Z"
                        }
                    ],
                    "pivot": {
                        "user_id": "1",
                        "track_id": "1",
                        "status": "0",
                        "created_at": "2021-01-04T09:49:50.000000Z",
                        "updated_at": "2021-01-04T09:49:50.000000Z"
                    }
                },
                
            ]
        },
        "tracks": [
            {
                "id": 1,
                "name": "track 1",
                "starting_lat": null,
                "starting_lng": null,
                "from": "Guwahati",
                "ending_lat": null,
                "ending_lng": null,
                "to": "Banglore",
                "distance": "1300",
                "kml_file": null,
                "status": "0",
                "created_at": "2021-01-04T09:49:47.000000Z",
                "updated_at": "2021-01-04T09:49:47.000000Z",
                "spent_time": [
                    {
                        "id": 9,
                        "start_time": "2012-07-17T09:21:49.000000Z",
                        "end_time": "2012-07-17T19:57:48.000000Z",
                        "user_id": "1",
                        "track_id": "1",
                        "created_at": "2021-01-04T09:49:55.000000Z",
                        "updated_at": "2021-01-04T09:49:55.000000Z"
                    }
                ],
                "pivot": {
                    "user_id": "1",
                    "track_id": "1",
                    "status": "0",
                    "created_at": "2021-01-04T09:49:50.000000Z",
                    "updated_at": "2021-01-04T09:49:50.000000Z"
                }
            },
           
            }
        ]
    }
}
1 like
newbie360's avatar

what the result if change the order

        $data['user'] = $user;
        $tracks = $user->tracks;
Tray2's avatar

And what do you get if you do this?

$user = User::first()->with('tracks');
public function get_user_details(Request $request, User $user)
    {
        return send_response(200, User::first->with('tracks'), 'success', 200);
    }
newbie360's avatar

i guess in the User model added some extra code to append the relationship

Please or to participate in this conversation.