Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

I want to use JSON Response _id #3241

Unanswered
savasdersimcelik asked this question in General
Discussion options

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"
 }
}
You must be logged in to vote

Replies: 2 comments 1 reply

Comment options

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;
 }
}
You must be logged in to vote
0 replies

This comment was marked as disruptive content.

Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Converted from issue

This discussion was converted from issue #3240 on January 07, 2025 13:41.

AltStyle によって変換されたページ (->オリジナル) /