Skip to content

Commit 0b97b2a

Browse files
authored
Merge branch '4.2' into merge-4.1-into-4.2-1712161920986
2 parents d4e5e2b + e338fc2 commit 0b97b2a

File tree

15 files changed

+359
-33
lines changed

15 files changed

+359
-33
lines changed

.github/workflows/build-ci.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ jobs:
2424
- "8.2"
2525
- "8.3"
2626
laravel:
27-
- "10.*"
27+
- "10.*"
28+
- "11.*"
2829
include:
2930
- php: "8.1"
3031
laravel: "10.*"
3132
mongodb: "5.0"
3233
mode: "low-deps"
34+
exclude:
35+
- php: "8.1"
36+
laravel: "11.*"
3337

3438
steps:
3539
- uses: "actions/checkout@v4"
@@ -76,8 +80,7 @@ jobs:
7680
restore-keys: "${{ matrix.os }}-composer-"
7781

7882
- name: "Install dependencies"
79-
run: composer update --no-interaction $([[ "${{ matrix.mode }}" == low-deps ]] && echo ' --prefer-lowest --prefer-stable')
80-
83+
run: composer update --no-interaction $([[ "${{ matrix.mode }}" == low-deps ]] && echo ' --prefer-lowest')
8184
- name: "Run tests"
8285
run: "./vendor/bin/phpunit --coverage-clover coverage.xml"
8386
env:

CHANGELOG.md

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

4+
## [unreleased]
5+
6+
7+
## [4.2.0] - 2024-12-14
8+
9+
* Add support for Laravel 11 by @GromNaN in [#2735](https://github.com/mongodb/laravel-mongodb/pull/2735)
10+
* Implement Model::createOrFirst() using findOneAndUpdate operation by @GromNaN in [#2742](https://github.com/mongodb/laravel-mongodb/pull/2742)
11+
412
## [4.1.3] - 2024-03-05
513

614
* Fix the timezone of `datetime` fields when they are read from the database. By @GromNaN in [#2739](https://github.com/mongodb/laravel-mongodb/pull/2739)

composer.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@
2424
"require": {
2525
"php": "^8.1",
2626
"ext-mongodb": "^1.15",
27-
"illuminate/support": "^10.0",
28-
"illuminate/container": "^10.0",
29-
"illuminate/database": "^10.30",
30-
"illuminate/events": "^10.0",
27+
"illuminate/support": "^10.0|^11",
28+
"illuminate/container": "^10.0|^11",
29+
"illuminate/database": "^10.30|^11",
30+
"illuminate/events": "^10.0|^11",
3131
"mongodb/mongodb": "^1.15"
3232
},
3333
"require-dev": {
3434
"phpunit/phpunit": "^10.3",
35-
"orchestra/testbench": "^8.0",
35+
"orchestra/testbench": "^8.0|^9.0",
3636
"mockery/mockery": "^1.4.4",
3737
"doctrine/coding-standard": "12.0.x-dev",
3838
"spatie/laravel-query-builder": "^5.6",
3939
"phpstan/phpstan": "^1.10"
4040
},
41+
"minimum-stability": "dev",
4142
"replace": {
4243
"jenssegers/mongodb": "self.version"
4344
},
@@ -66,9 +67,6 @@
6667
"cs:fix": "phpcbf"
6768
},
6869
"config": {
69-
"platform": {
70-
"php": "8.1"
71-
},
7270
"allow-plugins": {
7371
"dealerdirect/phpcodesniffer-composer-installer": true
7472
}

docs/includes/framework-compatibility-laravel.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33
:stub-columns: 1
44

55
* - {+odm-short+} Version
6+
- Laravel 11.x
67
- Laravel 10.x
78
- Laravel 9.x
89

10+
* - 4.2
11+
- ✓
12+
- ✓
13+
-
14+
915
* - 4.1
16+
-
1017
- ✓
1118
-
1219

1320
* - 4.0
21+
-
1422
- ✓
1523
-
1624

docs/quick-start.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ that connects to a MongoDB deployment.
5353
.. tip::
5454

5555
You can download the complete web application project by cloning the
56-
`laravel-quickstart <https://github.com/mongodb-university/laravel-quickstart>`__
56+
`laravel-quickstart <https://github.com/mongodb-university/laravel-quickstart/>`__
5757
GitHub repository.
5858

5959
.. button:: Next: Download and Install

docs/quick-start/configure-mongodb.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,20 @@ Configure Your MongoDB Connection
4141

4242
// ...
4343

44-
.. step:: Add the Laravel MongoDB provider
44+
.. step:: Add the MongoDB provider
4545

46-
Open the ``app.php`` file in the ``config`` directory and
47-
add the following entry into the ``providers`` array:
46+
Open the ``providers.php`` file in the ``bootstrap`` directory and add
47+
the following entry into the array:
4848

4949
.. code-block::
5050

5151
MongoDB\Laravel\MongoDBServiceProvider::class,
5252

53+
.. tip::
54+
55+
To learn how to register the provider in Laravel 10.x, see
56+
`Registering Providers <https://laravel.com/docs/10.x/providers#registering-providers>`__.
57+
5358
After completing these steps, your Laravel web application is ready to
5459
connect to MongoDB.
5560

docs/quick-start/view-data.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ View MongoDB Data
6767

6868
public function show()
6969
{
70-
return view('browse_movies', [
71-
'movies' => Movie::where('runtime', '<', 60)
72-
->where('imdb.rating', '>', 8.5)
73-
->orderBy('imdb.rating', 'desc')
74-
->take(10)
75-
->get()
76-
]);
70+
return view('browse_movies', [
71+
'movies' => Movie::where('runtime', '<', 60)
72+
->where('imdb.rating', '>', 8.5)
73+
->orderBy('imdb.rating', 'desc')
74+
->take(10)
75+
->get()
76+
]);
7777
}
7878

7979
.. step:: Add a web route

docs/quick-start/write-data.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ Write Data to MongoDB
3232

3333
.. step:: Add an API route that calls the controller function
3434

35+
Generate an API route file by running the following command:
36+
37+
.. code-block:: bash
38+
39+
php artisan install:api
40+
41+
.. tip::
42+
43+
Skip this step if you are using Laravel 10.x because the file that
44+
the command generates already exists.
45+
3546
Import the controller and add an API route that calls the ``store()``
3647
method in the ``routes/api.php`` file:
3748

@@ -42,7 +53,7 @@ Write Data to MongoDB
4253
// ...
4354

4455
Route::resource('movies', MovieController::class)->only([
45-
'store'
56+
'store'
4657
]);
4758

4859

@@ -57,8 +68,8 @@ Write Data to MongoDB
5768

5869
class Movie extends Model
5970
{
60-
protected $connection = 'mongodb';
61-
protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot'];
71+
protected $connection = 'mongodb';
72+
protected $fillable = ['title', 'year', 'runtime', 'imdb', 'plot'];
6273
}
6374

6475
.. step:: Post a request to the API

src/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ private static function lookupVersion(): string
328328
try {
329329
return self::$version = InstalledVersions::getPrettyVersion('mongodb/laravel-mongodb');
330330
} catch (Throwable) {
331-
// Ignore exceptions and return unknown version
331+
return self::$version = 'error';
332332
}
333333
}
334334

src/Eloquent/Builder.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66

77
use Illuminate\Database\ConnectionInterface;
88
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
9+
use InvalidArgumentException;
910
use MongoDB\Driver\Cursor;
11+
use MongoDB\Laravel\Collection;
1012
use MongoDB\Laravel\Helpers\QueriesRelationships;
13+
use MongoDB\Laravel\Internal\FindAndModifyCommandSubscriber;
1114
use MongoDB\Model\BSONDocument;
15+
use MongoDB\Operation\FindOneAndUpdate;
1216

17+
use function array_intersect_key;
1318
use function array_key_exists;
1419
use function array_merge;
1520
use function collect;
@@ -183,6 +188,52 @@ public function raw($value = null)
183188
return $results;
184189
}
185190

191+
/**
192+
* Attempt to create the record if it does not exist with the matching attributes.
193+
* If the record exists, it will be returned.
194+
*
195+
* @param array $attributes The attributes to check for duplicate records
196+
* @param array $values The attributes to insert if no matching record is found
197+
*/
198+
public function createOrFirst(array $attributes = [], array $values = []): Model
199+
{
200+
if ($attributes === []) {
201+
throw new InvalidArgumentException('You must provide attributes to check for duplicates');
202+
}
203+
204+
// Apply casting and default values to the attributes
205+
// In case of duplicate key between the attributes and the values, the values have priority
206+
$instance = $this->newModelInstance($values + $attributes);
207+
$values = $instance->getAttributes();
208+
$attributes = array_intersect_key($attributes, $values);
209+
210+
return $this->raw(function (Collection $collection) use ($attributes, $values) {
211+
$listener = new FindAndModifyCommandSubscriber();
212+
$collection->getManager()->addSubscriber($listener);
213+
214+
try {
215+
$document = $collection->findOneAndUpdate(
216+
$attributes,
217+
// Before MongoDB 5.0, $setOnInsert requires a non-empty document.
218+
// This is should not be an issue as $values includes the query filter.
219+
['$setOnInsert' => (object) $values],
220+
[
221+
'upsert' => true,
222+
'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
223+
'typeMap' => ['root' => 'array', 'document' => 'array'],
224+
],
225+
);
226+
} finally {
227+
$collection->getManager()->removeSubscriber($listener);
228+
}
229+
230+
$model = $this->model->newFromBuilder($document);
231+
$model->wasRecentlyCreated = $listener->created;
232+
233+
return $model;
234+
});
235+
}
236+
186237
/**
187238
* Add the "updated at" column to an array of values.
188239
* TODO Remove if https://github.com/laravel/framework/commit/6484744326531829341e1ff886cc9b628b20d73e

0 commit comments

Comments
 (0)