Skip to content

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

Merged
merged 9 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/includes/usage-examples/InsertOneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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,
]);
Comment on lines +23 to +27
Copy link
Member

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):

$movie = new Movie([
    'title' => 'Marriage Story',
    'year' => 2019,
    'runtime' => 136,
]);
$movie->save();

Copy link
Contributor

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 shows save()? If save() seems more common and helpful, we could update the example to show that method of inserting a single document instead.

Copy link
Member

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 (using findOneAndUpdate with upsert)


echo $movie->toJson();
// end-insert-one

$this->assertInstanceOf(Movie::class, $movie);
$this->expectOutputRegex('/^{"title":"Marriage Story","year":2019,"runtime":136,"updated_at":".{27}","created_at":".{27}","_id":"[a-z0-9]{24}"}$/');
}
}
1 change: 1 addition & 0 deletions docs/usage-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ calls the controller function and returns the result to a web interface.

/usage-examples/findOne
/usage-examples/updateOne
/usage-examples/insertOne
/usage-examples/deleteOne
73 changes: 73 additions & 0 deletions docs/usage-examples/insertOne.txt
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 query builder.

To insert a document, pass the data you need to insert as a document containing
the fields and values to the ``create()`` method.

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: json
:visible: false

{
"title": "Marriage Story",
"year": 2019,
"runtime": 136,
"updated_at": "...",
"created_at": "...",
"_id": "..."
}

To learn how to edit your Laravel application to run the usage example, see the
:ref:`Usage Examples landing page <laravel-usage-examples>`.

.. 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.