Skip to content

Commit 265d29c

Browse files
author
Chris Cho
committed
grammar and spelling corrections
1 parent f7e6f80 commit 265d29c

File tree

2 files changed

+67
-54
lines changed

2 files changed

+67
-54
lines changed

docs/eloquent-models/schema-builder.txt

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Schema Builder
99
:values: tutorial
1010

1111
.. meta::
12-
:keywords: php framework, odm, code example, schema facade, php artisan, eloquent
12+
:keywords: php framework, odm, code example, schema facade, php artisan, eloquent, blueprint, artisan, migrate
1313

1414
Overview
1515
--------
@@ -28,8 +28,15 @@ The following sections describe the Laravel schema builder and functionality
2828
available in {+odm-short+} and show examples of how to use them:
2929

3030
- :ref:`<laravel-eloquent-migrations>`
31+
- :ref:`<laravel-eloquent-collection-exists>`
3132
- :ref:`<laravel-eloquent-indexes>`
32-
- :ref:`<laravel-eloquent-collections>`
33+
34+
.. note::
35+
36+
{+odm-short+} supports managing indexes and collection, but
37+
excludes support for MongoDB JSON schemas for data validation. To learn
38+
more about JSON schema validation, see :manual:`Schema Validation </core/schema-validation/>`
39+
in the MongoDB server manual.
3340

3441
.. _laravel-eloquent-migrations:
3542

@@ -58,9 +65,8 @@ following changes to perform the schema changes on your MongoDB database:
5865
Make sure you set ``DB_CONNECTION=mongodb`` in your ``.env`` configuration
5966
file so that PHP artisan performs the migration on the correct database.
6067

61-
The following example migration class file that creates a collection and
62-
index when you run the migration and drops it when you reverse, or roll back
63-
the migration:
68+
The following example migration creates a collection and index when you run
69+
the migration and drops it when you roll back the migration:
6470

6571
.. literalinclude:: /includes/schema-builder/astronauts_migration.php
6672
:language: php
@@ -93,18 +99,35 @@ To learn more about Laravel migrations, see
9399
`Database: Migrations <https://laravel.com/docs/{+laravel-docs-version+}/migrations>`__
94100
in the Laravel documentation.
95101

102+
.. _laravel-eloquent-collection-exists:
103+
104+
Check Whether a Collection Exists
105+
---------------------------------
106+
107+
To check whether a collection exists, call the ``hasCollection()`` method on
108+
the ``Schema`` facade in your migration file. You can use this to
109+
perform migration logic conditionally.
110+
111+
The following example migration creates a ``stars`` collection if a collection
112+
named ``telescopes`` exists:
113+
114+
.. literalinclude:: /includes/schema-builder/stars_migration.php
115+
:language: php
116+
:start-after: begin conditional create
117+
:end-before: end conditional create
118+
96119
.. _laravel-eloquent-indexes:
97120

98121
Manage Indexes
99122
--------------
100123

101124
MongoDB indexes are data structures that improve query efficiency by reducing
102-
the number of documents needed to retrieve results of a query. Indexes such as
103-
geospatial indexes extend the ways in which you can query the data.
125+
the number of documents needed to retrieve query results. Indexes such as
126+
geospatial indexes extend how you can query the data.
104127

105-
To improve query performance by using an index, make sure the query is
106-
covered by the index. To learn more about indexes and query optimization, see
107-
the following MongoDB server manual pages:
128+
To improve query performance by using an index, make sure the index covers
129+
the query. To learn more about indexes and query optimization, see the
130+
following MongoDB server manual entries:
108131

109132
- :manual:`Indexes </indexes>`
110133
- :manual:`Query Optimization </core/query-optimization/>`
@@ -203,7 +226,6 @@ field:
203226
}
204227
]
205228

206-
207229
To learn more about index options, see :manual:`Options for All Index Types </reference/method/db.collection.createIndex/#options-for-all-index-types>`
208230
in the MongoDB server manual.
209231

@@ -213,10 +235,10 @@ Create Sparse, TTL, and Unique Indexes
213235
You can use {+odm-short+} helper methods to create the following types of
214236
indexes:
215237

216-
- Sparse indexes which allow index entries only for documents that contain the
238+
- Sparse indexes, which allow index entries only for documents that contain the
217239
specified field
218-
- Time-to-live (TTL) indexes which expire after a set amount of time
219-
- Unique indexes which prevent inserting documents that contain duplicate
240+
- Time-to-live (TTL) indexes, which expire after a set amount of time
241+
- Unique indexes, which prevent inserting documents that contain duplicate
220242
values for the indexed field
221243

222244
To create these index types, call the ``create()`` method on the ``Schema`` facade
@@ -225,7 +247,7 @@ method with a ``MongoDB\Laravel\Schema\Blueprint`` parameter. Call the
225247
appropriate helper method on the ``Blueprint`` instance and pass the
226248
index creation details.
227249

228-
The following migration code shows how to create a sparse index and TTL index
250+
The following migration code shows how to create a sparse and TTL index
229251
by using the index helpers. Click the :guilabel:`VIEW OUTPUT` button to see
230252
the indexes created by running the migration, including the default index on
231253
the ``_id`` field:
@@ -253,8 +275,8 @@ the ``_id`` field:
253275
}
254276
]
255277

256-
You can specify sparse, TTL, and unique indexes on a single field or compound
257-
index by specifying them in the index options.
278+
You can specify sparse, TTL, and unique indexes on either a single field or
279+
compound index by specifying them in the index options.
258280

259281
The following migration code shows how to create all three types of indexes
260282
on a single field. Click the :guilabel:`VIEW OUTPUT` button to see the indexes
@@ -353,41 +375,4 @@ from the ``flights`` collection:
353375
:start-after: begin drop index
354376
:end-before: end drop index
355377

356-
.. _laravel-eloquent-collections:
357-
358-
Manage Collections
359-
------------------
360-
361-
Check if a collection exists: Schema::hasCollection(<collection name>)
362-
363-
TODO
364-
365-
Note that creating a collection is a noop unless it also creates an index
366-
since MongoDB automatically creates collections when you first write to them.
367-
368-
Notes:
369-
370-
- collection (what does this method do?)
371-
Schema::hasCollection(<collection name>); check whether a collection exists
372-
373-
- determining column existence
374-
- drop collection public function dropIfExists($table)
375-
{
376-
if ($this->hasCollection($table)) {
377-
$this->drop($table);
378-
}
379-
}
380-
381-
index($columns = null, $name = null, $algorithm = null, $options = [])
382-
dropIndex($index = null)
383-
public function dropIndexIfExists($indexOrColumns = null)\
384-
public function hasIndex($indexOrColumns = null)
385-
386-
.. note::
387-
388-
Although MongoDB collections can be schemaless, they can use JSON schemas
389-
for data validation. The {+odm-short+} schema builder does not offer methods
390-
for data validation schemas. To learn more about JSON schema validation,
391-
see :manual:`Schema Validation </core/schema-validation/>` in the
392-
MongoDB server documentation.
393378

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use MongoDB\Laravel\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
10+
protected $connect = 'mongodb';
11+
12+
public function up(): void
13+
{
14+
// start conditional create
15+
$hasCollection = Schema::hasCollection('stars');
16+
17+
if ($hasCollection) {
18+
Schema::create('telescopes');
19+
}
20+
// end conditional create
21+
}
22+
23+
24+
public function down(): void
25+
{
26+
Schema::drop('stars');
27+
}
28+
};

0 commit comments

Comments
 (0)