Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e955aaf

Browse files
Merge 4.2 into 4.3 (#2874)
2 parents af05600 + b1ef93d commit e955aaf

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers;
6+
7+
use App\Models\Movie;
8+
use DateTimeImmutable;
9+
use MongoDB\BSON\UTCDateTime;
10+
use MongoDB\Laravel\Tests\TestCase;
11+
12+
class InsertManyTest extends TestCase
13+
{
14+
/**
15+
* @runInSeparateProcess
16+
* @preserveGlobalState disabled
17+
*/
18+
public function testInsertMany(): void
19+
{
20+
require_once __DIR__ . '/Movie.php';
21+
22+
Movie::truncate();
23+
24+
// begin-insert-many
25+
$success = Movie::insert([
26+
[
27+
'title' => 'Anatomy of a Fall',
28+
'release_date' => new UTCDateTime(new DateTimeImmutable('2023年08月23日')),
29+
],
30+
[
31+
'title' => 'The Boy and the Heron',
32+
'release_date' => new UTCDateTime(new DateTimeImmutable('2023年12月08日')),
33+
],
34+
[
35+
'title' => 'Passages',
36+
'release_date' => new UTCDateTime(new DateTimeImmutable('2023年06月28日')),
37+
],
38+
]);
39+
40+
echo 'Insert operation success: ' . ($success ? 'yes' : 'no');
41+
// end-insert-many
42+
43+
$this->assertTrue($success);
44+
$this->expectOutputString('Insert operation success: yes');
45+
}
46+
}

‎docs/usage-examples.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ calls the controller function and returns the result to a web interface.
7272
:maxdepth: 1
7373

7474
/usage-examples/findOne
75+
/usage-examples/insertMany
7576
/usage-examples/updateOne
7677
/usage-examples/deleteOne
7778
/usage-examples/deleteMany
7879
/usage-examples/count
7980
/usage-examples/distinct
80-

‎docs/usage-examples/insertMany.txt‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.. _laravel-insert-many-usage:
2+
3+
=========================
4+
Insert Multiple Documents
5+
=========================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: insert many, add, create, bulk, code example
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 1
18+
:class: singlecol
19+
20+
You can insert multiple documents into a collection by calling the ``insert()``
21+
method on an Eloquent model or a query builder.
22+
23+
To insert multiple documents, call the ``insert()`` method and specify the new documents
24+
as an array inside the method call. Each array entry contains a single document's field
25+
values.
26+
27+
Example
28+
-------
29+
30+
This usage example performs the following actions:
31+
32+
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
33+
``sample_mflix`` database
34+
- Inserts documents into the ``movies`` collection
35+
- Prints the result of the insert operation
36+
37+
The example calls the ``insert()`` method to insert documents that represent movies released
38+
in 2023. This method returns a value of ``1`` if the operation is successful, and it throws
39+
an exception if the operation is unsuccessful.
40+
41+
.. io-code-block::
42+
:copyable: true
43+
44+
.. input:: ../includes/usage-examples/InsertManyTest.php
45+
:start-after: begin-insert-many
46+
:end-before: end-insert-many
47+
:language: php
48+
:dedent:
49+
50+
.. output::
51+
:language: console
52+
:visible: false
53+
54+
Insert operation success: yes
55+
56+
To learn how to edit your Laravel application to run the usage example, see the
57+
:ref:`Usage Examples landing page <laravel-usage-examples>`.
58+
59+
.. tip::
60+
61+
To learn more about insert operations, see the :ref:`laravel-fundamentals-insert-documents` section
62+
of the Write Operations guide.
63+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /