-
Notifications
You must be signed in to change notification settings - Fork 1.5k
I want to use JSON Response _id #3241
Unanswered
savasdersimcelik
asked this question in
General
-
return response()->json([
'invoice' => Invoice::where('_user_id', '677bc826c707fd89b70ce9ed')->first()
]);
With the example code above, I am returning a response to the mobile application API.
The response output is as follows:
Previously, in our mobile application, we accessed it with _id, but now it is all changed to id.
My problem is that I want to use _id in the JSON responses.
{
"invoice": {
"_company_id": null,
"_user_id": "677bc826c707fd89b70ce9ed",
"number": 4015097219,
"name": "Savaş Dersim Çelik",
"first_name": "Savaş Dersim",
"last_name": "Çelik",
"business_name": "SDC Yazılım",
"tax_office": "Çanakkale",
"tax_number": "3479509471",
"accounting_mail": "savas.celik@mealbox.com.tr",
"phone_prefix": "+90",
"phone": "5178614619",
"city_name": "İstanbul",
"district_name": "Kadıköy",
"address": null,
"location": [],
"latitude": "0.00",
"longitude": "0.00",
"erp_code": 0,
"postcode": null,
"is_active": true,
"notes": "",
"is_individual": false,
"deleted_at": null,
"_created_by": "677bc826c707fd89b70ce9ed",
"updated_at": "2025-01-06T20:28:48.887000Z",
"created_at": "2025-01-06T20:28:48.887000Z",
"id": "677c3d001c41ec816d0d6a1f"
}
}
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 1 reply
-
You can change the representation of the model to rename id
to _id
by overriding the toArray
method of your model class:
class Invoice { // ... public function toArray() { $values = parent::toArray(); if (array_key_exists('id', $values)) { $values['_id'] = $values['id']; unset($values['id']); } return $values; } }
Beta Was this translation helpful? Give feedback.
All reactions
0 replies
This comment was marked as disruptive content.
This comment was marked as disruptive content.
-
Please respect our code of conduct.
You can check related issues where answers have been given:
Beta Was this translation helpful? Give feedback.
All reactions
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment