Skip to content

Commit aaf3982

Browse files
authored
Merge branch '5.0' into merge-4.8-into-5.0-1724777129234
2 parents 6a8a2e3 + 457971b commit aaf3982

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+683
-590
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [5.0.0] - next
5+
6+
* **BREAKING CHANGE** Use `id` as an alias for `_id` in commands and queries for compatibility with Eloquent packages by @GromNaN in [#3040](https://github.com/mongodb/laravel-mongodb/pull/3040)
7+
48
## [4.8.0] - 2024-08-27
59

610
* Add `Query\Builder::incrementEach()` and `decrementEach()` methods by @SmallRuralDog in [#2550](https://github.com/mongodb/laravel-mongodb/pull/2550)

docs/includes/auth/PersonalAccessToken.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ class PersonalAccessToken extends SanctumToken
1111

1212
protected $connection = 'mongodb';
1313
protected $table = 'personal_access_tokens';
14-
protected $primaryKey = '_id';
1514
protected $keyType = 'string';
1615
}

docs/includes/eloquent-models/PlanetThirdParty.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ class Planet extends CelestialBody
1010
use DocumentModel;
1111

1212
protected $fillable = ['name', 'diameter'];
13-
protected $primaryKey = '_id';
1413
protected $keyType = 'string';
1514
}

docs/includes/fundamentals/write-operations/WriteOperationsTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function testModelUpdateFluent(): void
162162

163163
// begin model update one fluent
164164
$concert = Concert::where(['performer' => 'Brad Mehldau'])
165-
->orderBy('_id')
165+
->orderBy('id')
166166
->first()
167167
->update(['venue' => 'Manchester Arena', 'ticketsSold' => 9543]);
168168
// end model update one fluent
@@ -370,31 +370,31 @@ public function testModelDeleteById(): void
370370

371371
$data = [
372372
[
373-
'_id' => 'CH-0401242000',
373+
'id' => 'CH-0401242000',
374374
'performer' => 'Mitsuko Uchida',
375375
'venue' => 'Carnegie Hall',
376376
'genres' => ['classical'],
377377
'ticketsSold' => 2121,
378378
'performanceDate' => new UTCDateTime(Carbon::create(2024, 4, 1, 20, 0, 0, 'EST')),
379379
],
380380
[
381-
'_id' => 'MSG-0212252000',
381+
'id' => 'MSG-0212252000',
382382
'performer' => 'Brad Mehldau',
383383
'venue' => 'Philharmonie de Paris',
384384
'genres' => [ 'jazz', 'post-bop' ],
385385
'ticketsSold' => 5745,
386386
'performanceDate' => new UTCDateTime(Carbon::create(2025, 2, 12, 20, 0, 0, 'CET')),
387387
],
388388
[
389-
'_id' => 'MSG-021222000',
389+
'id' => 'MSG-021222000',
390390
'performer' => 'Billy Joel',
391391
'venue' => 'Madison Square Garden',
392392
'genres' => [ 'rock', 'soft rock', 'pop rock' ],
393393
'ticketsSold' => 12852,
394394
'performanceDate' => new UTCDateTime(Carbon::create(2025, 2, 12, 20, 0, 0, 'CET')),
395395
],
396396
[
397-
'_id' => 'SF-06302000',
397+
'id' => 'SF-06302000',
398398
'performer' => 'The Rolling Stones',
399399
'venue' => 'Soldier Field',
400400
'genres' => [ 'rock', 'pop', 'blues' ],
@@ -478,22 +478,22 @@ public function testModelDeleteMultipleById(): void
478478
Concert::truncate();
479479
$data = [
480480
[
481-
'_id' => 3,
481+
'id' => 3,
482482
'performer' => 'Mitsuko Uchida',
483483
'venue' => 'Carnegie Hall',
484484
],
485485
[
486-
'_id' => 5,
486+
'id' => 5,
487487
'performer' => 'Brad Mehldau',
488488
'venue' => 'Philharmonie de Paris',
489489
],
490490
[
491-
'_id' => 7,
491+
'id' => 7,
492492
'performer' => 'Billy Joel',
493493
'venue' => 'Madison Square Garden',
494494
],
495495
[
496-
'_id' => 9,
496+
'id' => 9,
497497
'performer' => 'The Rolling Stones',
498498
'venue' => 'Soldier Field',
499499
],

docs/includes/usage-examples/DeleteOneTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testDeleteOne(): void
2727

2828
// begin-delete-one
2929
$deleted = Movie::where('title', 'Quiz Show')
30-
->orderBy('_id')
30+
->orderBy('id')
3131
->limit(1)
3232
->delete();
3333

docs/includes/usage-examples/FindManyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testFindMany(): void
3535

3636
// begin-find
3737
$movies = Movie::where('runtime', '>', 900)
38-
->orderBy('_id')
38+
->orderBy('id')
3939
->get();
4040
// end-find
4141

docs/includes/usage-examples/FindOneTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ public function testFindOne(): void
2424

2525
// begin-find-one
2626
$movie = Movie::where('directors', 'Rob Reiner')
27-
->orderBy('_id')
27+
->orderBy('id')
2828
->first();
2929

3030
echo $movie->toJson();
3131
// end-find-one
3232

3333
$this->assertInstanceOf(Movie::class, $movie);
34-
$this->expectOutputRegex('/^{"_id":"[a-z0-9]{24}","title":"The Shawshank Redemption","directors":\["Frank Darabont","Rob Reiner"\]}$/');
34+
$this->expectOutputRegex('/^{"_id":"[a-z0-9]{24}","title":"The Shawshank Redemption","directors":\["Frank Darabont","Rob Reiner"\],"id":"[a-z0-9]{24}"}$/');
3535
}
3636
}

docs/includes/usage-examples/InsertOneTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ public function testInsertOne(): void
3030
// end-insert-one
3131

3232
$this->assertInstanceOf(Movie::class, $movie);
33-
$this->expectOutputRegex('/^{"title":"Marriage Story","year":2019,"runtime":136,"updated_at":".{27}","created_at":".{27}","_id":"[a-z0-9]{24}"}$/');
33+
$this->expectOutputRegex('/^{"title":"Marriage Story","year":2019,"runtime":136,"updated_at":".{27}","created_at":".{27}","id":"[a-z0-9]{24}"}$/');
3434
}
3535
}

docs/includes/usage-examples/UpdateOneTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testUpdateOne(): void
3030

3131
// begin-update-one
3232
$updates = Movie::where('title', 'Carol')
33-
->orderBy('_id')
33+
->orderBy('id')
3434
->first()
3535
->update([
3636
'imdb' => [

src/Auth/User.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ class User extends BaseUser
1111
{
1212
use DocumentModel;
1313

14-
protected $primaryKey = '_id';
1514
protected $keyType = 'string';
1615
}

src/Connection.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
use function is_array;
2323
use function preg_match;
2424
use function str_contains;
25-
use function trigger_error;
2625

27-
use const E_USER_DEPRECATED;
2826
use const FILTER_FLAG_IPV6;
2927
use const FILTER_VALIDATE_IP;
3028

@@ -77,22 +75,6 @@ public function __construct(array $config)
7775
$this->useDefaultQueryGrammar();
7876
}
7977

80-
/**
81-
* Begin a fluent query against a database collection.
82-
*
83-
* @deprecated since mongodb/laravel-mongodb 4.8, use the function table() instead
84-
*
85-
* @param string $collection
86-
*
87-
* @return Query\Builder
88-
*/
89-
public function collection($collection)
90-
{
91-
@trigger_error('Since mongodb/laravel-mongodb 4.8, the method Connection::collection() is deprecated and will be removed in version 5.0. Use the table() method instead.', E_USER_DEPRECATED);
92-
93-
return $this->table($collection);
94-
}
95-
9678
/**
9779
* Begin a fluent query against a database collection.
9880
*

src/Eloquent/DocumentModel.php

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@
4646
use function str_contains;
4747
use function str_starts_with;
4848
use function strcmp;
49-
use function trigger_error;
49+
use function strlen;
5050
use function var_export;
5151

52-
use const E_USER_DEPRECATED;
53-
5452
trait DocumentModel
5553
{
5654
use HybridRelations;
@@ -79,9 +77,7 @@ public function getIdAttribute($value = null)
7977
{
8078
// If we don't have a value for 'id', we will use the MongoDB '_id' value.
8179
// This allows us to work with models in a more sql-like way.
82-
if (! $value && array_key_exists('_id', $this->attributes)) {
83-
$value = $this->attributes['_id'];
84-
}
80+
$value ??= $this->attributes['id'] ?? $this->attributes['_id'] ?? null;
8581

8682
// Convert ObjectID to string.
8783
if ($value instanceof ObjectID) {
@@ -141,18 +137,6 @@ public function freshTimestamp()
141137
return new UTCDateTime(Date::now());
142138
}
143139

144-
/** @inheritdoc */
145-
public function getTable()
146-
{
147-
if (isset($this->collection)) {
148-
trigger_error('Since mongodb/laravel-mongodb 4.8: Using "$collection" property is deprecated. Use "$table" instead.', E_USER_DEPRECATED);
149-
150-
return $this->collection;
151-
}
152-
153-
return parent::getTable();
154-
}
155-
156140
/** @inheritdoc */
157141
public function getAttribute($key)
158142
{
@@ -248,10 +232,8 @@ public function setAttribute($key, $value)
248232
}
249233

250234
// Convert _id to ObjectID.
251-
if ($key === '_id' && is_string($value)) {
252-
$builder = $this->newBaseQueryBuilder();
253-
254-
$value = $builder->convertKey($value);
235+
if (($key === '_id' || $key === 'id') && is_string($value) && strlen($value) === 24) {
236+
$value = $this->newBaseQueryBuilder()->convertKey($value);
255237
}
256238

257239
// Support keys in dot notation.
@@ -729,12 +711,16 @@ protected function isBSON(mixed $value): bool
729711
*/
730712
public function save(array $options = [])
731713
{
732-
// SQL databases would use autoincrement the id field if set to null.
714+
// SQL databases would autoincrement the id field if set to null.
733715
// Apply the same behavior to MongoDB with _id only, otherwise null would be stored.
734716
if (array_key_exists('_id', $this->attributes) && $this->attributes['_id'] === null) {
735717
unset($this->attributes['_id']);
736718
}
737719

720+
if (array_key_exists('id', $this->attributes) && $this->attributes['id'] === null) {
721+
unset($this->attributes['id']);
722+
}
723+
738724
$saved = parent::save($options);
739725

740726
// Clear list of unset fields

src/Eloquent/Model.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ abstract class Model extends BaseModel
1616
{
1717
use DocumentModel;
1818

19-
/**
20-
* The primary key for the model.
21-
*
22-
* @var string
23-
*/
24-
protected $primaryKey = '_id';
25-
2619
/**
2720
* The primary key type.
2821
*

0 commit comments

Comments
 (0)