I would do
$user = User::first()->with('tracks');
And not do it twice like you are doing.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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?
I would do
$user = User::first()->with('tracks');
And not do it twice like you are doing.
@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.
Show me the controller code and the view code.
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"
}
},
}
]
}
}
what the result if change the order
$data['user'] = $user;
$tracks = $user->tracks;
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);
}
i guess in the User model added some extra code to append the relationship
Please or to participate in this conversation.