Skip to content

Commit af3fbb9

Browse files
committed
_id bug
1 parent 8f22c78 commit af3fbb9

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this `laravel-elasticsearch` package will be documented in this file.
44

5+
## v5.4.1 - 2026-02-23
6+
7+
### Fixed
8+
9+
- `_id` no longer leaks into serialized output (`toArray()`, `toJson()`). The internal `_id` metadata field was being exposed alongside `id`, resulting in duplicate ID fields in model serialization.
10+
511
## v5.4.0 - 2026-02-21
612

713
This release is compatible with Laravel 10, 11 & 12
@@ -95,7 +101,8 @@ Why: When you need IDs that sort chronologically across multiple processes/worke
95101
- Test suite expanded from 379 to 422 tests (2,548 assertions), all passing
96102

97103
### Fixed
98-
- `id` is now always present in serialized model output
104+
- `id` is now always present in serialized model output (`toArray()`, `toJson()`)
105+
- `_id` is no longer exposed in serialized output (internal metadata stays internal)
99106
- Removed dead debug code from Connection.php
100107

101108
**Full Changelog**: https://github.com/pdphilip/laravel-elasticsearch/compare/v5.3.0...v5.4.0

src/Eloquent/ElasticsearchModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ public function attributesToArray(): array
105105
{
106106
$attributes = parent::attributesToArray();
107107

108-
// Ensure 'id' is always present in serialized output
109-
// ES stores document ID as '_id', but we want 'id' for consistency
108+
// ES stores document ID as '_id' metadata, but we want 'id' for consistency
110109
if (! isset($attributes['id'])) {
111110
$attributes['id'] = $this->id;
112111
}
112+
unset($attributes['_id']);
113113

114114
return $attributes;
115115
}

tests/ModelTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -707,25 +707,24 @@
707707
expect($fetched->id)->toBe($originalId)
708708
->and($fetched->_id)->toBe($originalId);
709709

710-
// Verify toArray() includes 'id' key
710+
// Verify toArray() includes 'id' but not '_id'
711711
$array = $fetched->toArray();
712712
expect($array)->toHaveKey('id')
713-
->and($array['id'])->toBe($originalId);
713+
->and($array['id'])->toBe($originalId)
714+
->and($array)->not->toHaveKey('_id');
714715

715-
// Both 'id' and '_id' should be present for backwards compatibility
716-
expect($array)->toHaveKey('_id')
717-
->and($array['_id'])->toBe($originalId);
718-
719-
// Verify toJson() includes 'id' key
716+
// Verify toJson() includes 'id' but not '_id'
720717
$json = json_decode($fetched->toJson(), true);
721718
expect($json)->toHaveKey('id')
722-
->and($json['id'])->toBe($originalId);
719+
->and($json['id'])->toBe($originalId)
720+
->and($json)->not->toHaveKey('_id');
723721

724722
// Verify collection serialization also includes 'id'
725723
$users = User::where('name', 'John Doe')->get();
726724
$collectionArray = $users->toArray();
727725
expect($collectionArray[0])->toHaveKey('id')
728-
->and($collectionArray[0]['id'])->toBe($originalId);
726+
->and($collectionArray[0]['id'])->toBe($originalId)
727+
->and($collectionArray[0])->not->toHaveKey('_id');
729728
});
730729

731730
it('tests _id is still accessible for backwards compatibility', function () {

0 commit comments

Comments
 (0)