Skip to content

DOCSP-45411: qb options #3208

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 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions docs/includes/query-builder/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ protected function tearDown(): void
parent::tearDown();
}

public function testOptions(): void
{
// begin options
$result = DB::connection('mongodb')
->table('movies')
->where('year', 2000)
->options(['comment' => 'hello'])
->get();
// end options

$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}

public function testWhere(): void
{
// begin query where
Expand Down
44 changes: 38 additions & 6 deletions docs/query-builder.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,27 @@ The following example shows the syntax of a query builder call:
DB::table('<collection name>')
// chain methods by using the "->" object operator
->get();

.. tip::

Before using the ``DB::table()`` method, ensure that you specify MongoDB as your application's
default database connection. For instructions on setting the database connection,
see the :ref:`laravel-quick-start-connect-to-mongodb` step in the Quick Start.
Before using the ``DB::table()`` method, ensure that you specify
MongoDB as your application's default database connection. For
instructions on setting the database connection, see the
:ref:`laravel-quick-start-connect-to-mongodb` step in the Quick
Start.

If MongoDB is not your application's default database, you can use the ``DB::connection()`` method
to specify a MongoDB connection. Pass the name of the connection to the ``connection()`` method,
as shown in the following code:
If MongoDB is not your application's default database, you can use
the ``DB::connection()`` method to specify a MongoDB connection. Pass
the name of the connection to the ``connection()`` method, as shown
in the following code:

.. code-block:: php

$connection = DB::connection('mongodb');

This guide provides examples of the following types of query builder operations:

- :ref:`laravel-options-query-builder`
- :ref:`laravel-retrieve-query-builder`
- :ref:`laravel-modify-results-query-builder`
- :ref:`laravel-mongodb-read-query-builder`
Expand All @@ -81,6 +86,33 @@ of the Quick Start.
To perform read and write operations by using the query builder, import the
``Illuminate\Support\Facades\DB`` facade and compose your query.

.. _laravel-options-query-builder:

Set Query-Level Options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: I think it makes more sense to have the "Retrieve Matching Documents" section come before this one, since "Set Query-Level Options" sets options for where(), described in "Retrieve Matching Documents". Maybe you could move this section before "Modify Query Results"?

-----------------------

You can modify the way that the {+odm-short+} performs queries by
setting options on the query builder. You can pass an array of options
to the ``options()`` query builder method to specify options for the
query.

The following code demonstrates how to attach a comment to
a query:

.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
:language: php
:dedent:
:start-after: begin options
:end-before: end options

The query builder accepts the same options that you can set for
the :phpmethod:`find() <phpmethod.MongoDB\\Collection::find()>` method in the
{+php-library+}. Some of the options to modify query results, such as
``skip``, ``sort``, and ``limit``, are settable directly as query
builder methods and are described in the
:ref:`laravel-modify-results-query-builder` section of this guide. We
recommend that you use these methods instead of passing them as options.

.. _laravel-retrieve-query-builder:

Retrieve Matching Documents
Expand Down