Skip to content

Commit aebf049

Browse files
authored
DOCSP-42957: DateTimeInterface in queries (#3140)
Adds information & a code example about automatic conversion from DateTimeInterface to UTCDateTime in queries.
1 parent f65b9e0 commit aebf049

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

docs/includes/query-builder/QueryBuilderTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Http\Controllers;
66

7+
use Carbon\Carbon;
78
use Illuminate\Database\Query\Builder;
89
use Illuminate\Pagination\AbstractPaginator;
910
use Illuminate\Support\Facades\DB;
@@ -148,14 +149,26 @@ public function testWhereIn(): void
148149
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
149150
}
150151

152+
public function testWhereCarbon(): void
153+
{
154+
// begin query where date
155+
$result = DB::connection('mongodb')
156+
->table('movies')
157+
->where('released', Carbon::create(2010, 1, 15))
158+
->get();
159+
// end query where date
160+
161+
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
162+
}
163+
151164
public function testWhereDate(): void
152165
{
153-
// begin query whereDate
166+
// begin query whereDate string
154167
$result = DB::connection('mongodb')
155168
->table('movies')
156169
->whereDate('released', '2010-1-15')
157170
->get();
158-
// end query whereDate
171+
// end query whereDate string
159172

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

docs/query-builder.txt

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,23 +353,38 @@ query builder method to retrieve documents from the
353353
Match Dates Example
354354
^^^^^^^^^^^^^^^^^^^
355355

356-
The following example shows how to use the ``whereDate()``
356+
The following example shows how to use the ``where()``
357357
query builder method to retrieve documents from the
358-
``movies`` collection that match the specified date of
359-
``2010-1-15`` in the ``released`` field:
358+
``movies`` collection in which the ``released`` value
359+
is January 15, 2010, specified in a ``Carbon`` object:
360360

361361
.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
362362
:language: php
363363
:dedent:
364-
:start-after: begin query whereDate
365-
:end-before: end query whereDate
364+
:start-after: begin query where date
365+
:end-before: end query where date
366366

367-
.. note:: Date Query Result Type
367+
.. note:: Date Query Filter and Result Type
368368

369-
Starting in {+odm-long+} v5.0, ``UTCDateTime`` BSON values in MongoDB
370-
are returned as `Carbon <https://carbon.nesbot.com/docs/>`__ date
371-
classes in query results. The {+odm-short+} applies the default
372-
timezone when performing this conversion.
369+
Starting in {+odm-long+} v5.0, `Carbon <https://carbon.nesbot.com/docs/>`__
370+
objects passed as query filters, as shown in the preceding code, are
371+
converted to ``UTCDateTime`` BSON values.
372+
373+
In query results, ``UTCDateTime`` BSON values in MongoDB are returned as ``Carbon``
374+
objects. The {+odm-short+} applies the default timezone when performing
375+
this conversion.
376+
377+
If you want to represent a date as a string in your query filter
378+
rather than as a ``Carbon`` object, use the ``whereDate()`` query
379+
builder method. The following example retrieves documents from
380+
the ``movies`` collection in which the ``released`` value
381+
is January 15, 2010 and specifies the date as a string:
382+
383+
.. literalinclude:: /includes/query-builder/QueryBuilderTest.php
384+
:language: php
385+
:dedent:
386+
:start-after: begin query whereDate string
387+
:end-before: end query whereDate string
373388

374389
.. _laravel-query-builder-pattern:
375390

docs/upgrade.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ This library version introduces the following breaking changes:
101101
use the default ``Illuminate\Queue\Failed\DatabaseFailedJobProvider``
102102
class and specify a connection to MongoDB.
103103

104+
- When using a ``DateTimeInterface`` object, including ``Carbon``, in a query,
105+
the library converts the ``DateTimeInterface`` to a ``MongoDB\BSON\UTCDateTime``
106+
object. This conversion applies to ``DateTimeInterface`` objects passed as query
107+
filters to the ``where()`` method or as data passed to the ``insert()`` and
108+
``update()`` methods.
109+
110+
To view an example that passes a ``Carbon`` object to the
111+
``DB::where()`` method, see the :ref:`laravel-query-builder-wheredate`
112+
section of the Query Builder guide.
113+
104114
- In query results, the library converts BSON ``UTCDateTime`` objects to ``Carbon``
105115
date classes, applying the default timezone.
106116

0 commit comments

Comments
 (0)