Closed Torquin closed 1 day ago
Exact, the raw()
method gives access to the MongoDB features directly. I didn't want to apply the transformation to the results of this function because it seems to me that it's designed for advanced use and that it's not compatible with Laravel and SQL databases.
What you can do is add the id
field and remove _id
from the projection.
\App\Models\Container::raw(function($collection) {
return $collection->findOne(
[], // filter
// Options
[
'projection' => [
// Remove the "_id" field
'_id' => -1,
// Add the "id" field, with the value if "_id"
'id' => '$_id',
// You must specify the other fields you want to retrieve
'name' => 1,
],
]
);
});
@GromNaN Thanks for your answer.
We tried to test your pending pull-request but we can't go further as it looks like raw() requests return BSON documents instead of eloquent models.
We also tested the same thing in mongodb 4.8.x & 5.0.0 and both (->where & ->raw) returned eloquent models.
Why do you want to use the raw()
method here?
If you have a specific filter that you can't or don't want to express with the Query Builder, you can pass it directly to where
.
$filter = [/* Complex query expression */];
\App\Models\Container::where($filter)->first()
This was just a simplified example of a raw request. We use complex aggregation built dynamically by our app (facets, projects, lookup, unwind etc). These requests were returning eloquent models that we used to call their methods.
Another thing is that this update would force dealing with 2 different data formats depending on whether using raw or classic query builder.
Hi @GromNaN ,
Another issue we've found is that the function "aliasIdForResult" works well (transforms all _id to id, nested or not) for class query builder. But when using with raw(), as raw() returns BSON documents, $values is neither an array nor a stdClass and so nothing is applied.
Thanks for your help
You need to change the typeMap to get data in array.
$collection->aggregate($pipeline, ['typeMap' => ['root' => 'object', 'document' => 'object', 'array' => 'array']);
Great one ! Ok we've resolved to fetching data & forceFilling our model. The typeMap did the job and all '_id' (even nested) are changed to 'id'. Thanks for bearing with us :)
@Torquin Can you check branch 5.x, it should handle the conversion automatically (for Eloquent Models only)
@GromNaN Thanks so much, this is perfect !
Description:
Following the latest update and id management, raw requests do not seem to return 'id' but still return '_id'
Steps to reproduce
Expected behaviour
Should receive 'id'
Actual behaviour
Receive '_id'