Skip to content

Merge 4.1 into 4.2 #2882

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
2 commits merged into from
Apr 19, 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,
]);

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}"}$/');
}
}
7 changes: 7 additions & 0 deletions docs/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Fundamentals
To learn how to perform the following tasks by using {+odm-short+},
see the following content:

- :ref:`Manage Databases and Collections <laravel-db-coll>`
- :ref:`laravel-fundamentals-read-ops`
- :ref:`laravel-fundamentals-write-ops`
- :ref:`laravel-eloquent-models`
Expand All @@ -75,6 +76,12 @@ Issues & Help
Learn how to report bugs, contribute to the library, and find
more resources in the :ref:`laravel-issues-and-help` section.

Feature Compatibility
---------------------

Learn about Laravel features that {+odm-short+} supports in the
:ref:`laravel-feature-compat` section.

Compatibility
-------------

Expand Down
1 change: 1 addition & 0 deletions docs/usage-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ calls the controller function and returns the result to a web interface.
/usage-examples/findOne
/usage-examples/insertMany
/usage-examples/updateOne
/usage-examples/insertOne
/usage-examples/deleteOne
/usage-examples/deleteMany
/usage-examples/count
Expand Down
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
of the Write Operations guide.