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 01dd49f

Browse files
DOCSP-35980: Insert One usage example (#2826)
* DOCSP-35980: Insert One usage example
1 parent f83541d commit 01dd49f

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Http\Controllers;
6+
7+
use App\Models\Movie;
8+
use MongoDB\Laravel\Tests\TestCase;
9+
10+
class InsertOneTest extends TestCase
11+
{
12+
/**
13+
* @runInSeparateProcess
14+
* @preserveGlobalState disabled
15+
*/
16+
public function testInsertOne(): void
17+
{
18+
require_once __DIR__ . '/Movie.php';
19+
20+
Movie::truncate();
21+
22+
// begin-insert-one
23+
$movie = Movie::create([
24+
'title' => 'Marriage Story',
25+
'year' => 2019,
26+
'runtime' => 136,
27+
]);
28+
29+
echo $movie->toJson();
30+
// end-insert-one
31+
32+
$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}"}$/');
34+
}
35+
}

‎docs/usage-examples.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ calls the controller function and returns the result to a web interface.
7474
/usage-examples/findOne
7575
/usage-examples/insertMany
7676
/usage-examples/updateOne
77+
/usage-examples/insertOne
7778
/usage-examples/deleteOne
7879
/usage-examples/deleteMany
7980
/usage-examples/count

‎docs/usage-examples/insertOne.txt‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
.. _laravel-insert-one-usage:
2+
3+
=================
4+
Insert a Document
5+
=================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: insert one, add one, code example
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 1
18+
:class: singlecol
19+
20+
You can insert a document into a collection by calling the ``create()`` method on
21+
an Eloquent model or query builder.
22+
23+
To insert a document, pass the data you need to insert as a document containing
24+
the fields and values to the ``create()`` method.
25+
26+
Example
27+
-------
28+
29+
This usage example performs the following actions:
30+
31+
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the
32+
``sample_mflix`` database
33+
- Inserts a document into the ``movies`` collection
34+
35+
The example calls the ``create()`` method to insert a document that contains the following
36+
information:
37+
38+
- ``title`` value of ``"Marriage Story"``
39+
- ``year`` value of ``2019``
40+
- ``runtime`` value of ``136``
41+
42+
.. io-code-block::
43+
:copyable: true
44+
45+
.. input:: ../includes/usage-examples/InsertOneTest.php
46+
:start-after: begin-insert-one
47+
:end-before: end-insert-one
48+
:language: php
49+
:dedent:
50+
51+
.. output::
52+
:language: json
53+
:visible: false
54+
55+
{
56+
"title": "Marriage Story",
57+
"year": 2019,
58+
"runtime": 136,
59+
"updated_at": "...",
60+
"created_at": "...",
61+
"_id": "..."
62+
}
63+
64+
To learn how to edit your Laravel application to run the usage example, see the
65+
:ref:`Usage Examples landing page <laravel-usage-examples>`.
66+
67+
.. tip::
68+
69+
You can also use the ``save()`` or ``insert()`` methods to insert a document into a collection.
70+
To learn more about insert operations, see the :ref:`laravel-fundamentals-insert-documents` section
71+
of the Write Operations guide.
72+
73+

0 commit comments

Comments
(0)

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