Skip to content

_id to id JSON reponse #3238

Closed as not planned
Closed as not planned
@savasdersimcelik

Description

@savasdersimcelik

-Laravel-mongodb Version: 5.1.0
-PHP Version: 8.3.0
-Database Driver & Version:

Description:

We store our primary keys in the database as _id, and throughout our project, we use $model->_id without issues. However, when using the following pattern:

$get = Model::all();
return response()->json(['data' => $get]);

The _id field causes problems, and APIs fail because _id cannot be directly handled in this context. To work around this issue, we have to perform a mapping, which is tedious.

We request a more permanent solution for this. Specifically, having the ability to define the primary key dynamically in the model (e.g., $primaryKey = '_id' or $primaryKey = 'id'). This would allow developers to continue using _id seamlessly without rewriting or remapping the data structure.

Steps to reproduce:

Set up a model with _id as the primary key in the database.
Query all records using Model::all().
Return the result as JSON using response()->json().
Expected behavior:
The _id field should be included and correctly handled when returning the data as JSON, without requiring additional mapping.

Actual behavior:

The _id field is not properly managed, and APIs fail unless we manually map it to id.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions