-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35980: Insert One usage example #2826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
a78ca45
327c046
c45c82e
8167050
ac9f022
1767824
d124f60
001abc5
54280ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Movie; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
|
||
class InsertOneTest extends TestCase | ||
{ | ||
/** | ||
* @runInSeparateProcess | ||
* @preserveGlobalState disabled | ||
*/ | ||
public function testInsertOne(): void | ||
{ | ||
require_once __DIR__ . '/Movie.php'; | ||
|
||
Movie::truncate(); | ||
|
||
// begin-insert-one | ||
$movie = Movie::create([ | ||
'title' => 'Marriage Story', | ||
'year' => 2019, | ||
'runtime' => 136, | ||
]); | ||
|
||
echo $movie->toJson(); | ||
// end-insert-one | ||
|
||
$this->assertInstanceOf(Movie::class, $movie); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,73 @@ | ||||||||||
.. _laravel-insert-one-usage: | ||||||||||
|
||||||||||
================= | ||||||||||
Insert a Document | ||||||||||
================= | ||||||||||
|
||||||||||
.. facet:: | ||||||||||
:name: genre | ||||||||||
:values: reference | ||||||||||
|
||||||||||
.. meta:: | ||||||||||
:keywords: insert one, add one, code example | ||||||||||
|
||||||||||
.. contents:: On this page | ||||||||||
:local: | ||||||||||
:backlinks: none | ||||||||||
:depth: 1 | ||||||||||
:class: singlecol | ||||||||||
|
||||||||||
You can insert a document into a collection by calling the ``create()`` method on | ||||||||||
an Eloquent model or on a query builder. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
To insert a document, call the ``create()`` method and specify the document's field values | ||||||||||
inside the method call. | ||||||||||
|
||||||||||
Example | ||||||||||
------- | ||||||||||
|
||||||||||
This usage example performs the following actions: | ||||||||||
|
||||||||||
- Uses the ``Movie`` Eloquent model to represent the ``movies`` collection in the | ||||||||||
``sample_mflix`` database | ||||||||||
- Inserts a document into the ``movies`` collection | ||||||||||
|
||||||||||
The example calls the ``create()`` method to insert a document that contains the following | ||||||||||
information: | ||||||||||
|
||||||||||
- ``title`` value of ``"Marriage Story"`` | ||||||||||
- ``year`` value of ``2019`` | ||||||||||
- ``runtime`` value of ``136`` | ||||||||||
|
||||||||||
.. io-code-block:: | ||||||||||
:copyable: true | ||||||||||
|
||||||||||
.. input:: ../includes/usage-examples/InsertOneTest.php | ||||||||||
:start-after: begin-insert-one | ||||||||||
:end-before: end-insert-one | ||||||||||
:language: php | ||||||||||
:dedent: | ||||||||||
|
||||||||||
.. output:: | ||||||||||
:language: console | ||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
:visible: false | ||||||||||
|
||||||||||
{ | ||||||||||
"title": "Marriage Story", | ||||||||||
"year": 2019, | ||||||||||
"runtime": 136, | ||||||||||
"updated_at": "...", | ||||||||||
"created_at": "...", | ||||||||||
"_id": "..." | ||||||||||
} | ||||||||||
|
||||||||||
For instructions on editing your Laravel application to run the usage example, see the | ||||||||||
:ref:`Usage Example landing page <laravel-usage-examples>`. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
.. tip:: | ||||||||||
|
||||||||||
You can also use the ``save()`` or ``insert()`` methods to insert a document into a collection. | ||||||||||
To learn more about insert operations, see the :ref:`laravel-fundamentals-insert-documents` section | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: will this be merged only after write operations is merged? Link is broken as of now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I'll wait until after write operations is merged |
||||||||||
of the Write Operations guide. | ||||||||||
|
||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example could be completed by the more expressive form (if anyone need to update the
$movie
model before saving):There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GromNaN Would you recommend replacing the
create()
example with one that showssave()
? Ifsave()
seems more common and helpful, we could update the example to show that method of inserting a single document instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No,
Movie::create([...])
is the most common way to create a model.There is also
Movie::createOrFirst()
that can be used (usingfindOneAndUpdate
with upsert)