Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f35d632

Browse files
DOCSP-41557: New v4.7 commands and methods (#3084)
* DOCSP-41557: v4.7 commands and methods * reword, fixes * RR feedback * JT review * wording * fix
1 parent 185d93a commit f35d632

File tree

1 file changed

+106
-5
lines changed

1 file changed

+106
-5
lines changed

‎docs/fundamentals/database-collection.txt‎

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,118 @@ as in the preceding example, but the query is constructed by using the
161161
List Collections
162162
----------------
163163

164-
To see information about each of the collections in a database, call the
165-
``listCollections()`` method.
164+
You can take either of the following actions to see information
165+
about the collections in a database:
166166

167-
The following example accesses a database connection, then
168-
calls the ``listCollections()`` method to retrieve information about the
169-
collections in the database:
167+
- :ref:`laravel-list-coll-command`
168+
- :ref:`laravel-list-coll-methods`
169+
170+
.. _laravel-list-coll-command:
171+
172+
Run a Shell Command
173+
~~~~~~~~~~~~~~~~~~~
174+
175+
You can list the collections in a database by running the following
176+
command in your shell from your project's root directory:
177+
178+
.. code-block:: bash
179+
180+
php artisan db:show
181+
182+
This command outputs information about the configured database and lists its
183+
collections under the ``Table`` header. For more information about the ``db:show``
184+
command, see `Laravel: New DB Commands <https://blog.laravel.com/laravel-new-db-commands-and-more>`__
185+
on the official Laravel blog.
186+
187+
.. _laravel-list-coll-methods:
188+
189+
Call Database or Schema Methods
190+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
191+
192+
You can list the collections in a database by calling the following
193+
methods in your application:
194+
195+
- ``DB::listCollections()``: lists information about each collection by
196+
using the query builder
197+
- ``Schema::getTablesListing()``: lists the name of each collection by
198+
using the schema builder
199+
- ``Schema::getTables()``: lists the name and size of each collection by
200+
using the schema builder
201+
202+
.. note::
203+
204+
MongoDB is a schemaless database, so the preceding schema builder methods
205+
query the database data rather than the schema.
206+
207+
Example
208+
```````
209+
210+
The following example accesses a database connection, then calls the
211+
``listCollections()`` query builder method to retrieve information about
212+
the collections in the database:
170213

171214
.. code-block:: php
172215

173216
$collections = DB::connection('mongodb')->getMongoDB()->listCollections();
174217

218+
List Collection Fields
219+
----------------------
220+
221+
You can take either of the following actions to see information
222+
about each field in a collection:
223+
224+
- :ref:`laravel-list-fields-command`
225+
- :ref:`laravel-list-fields-methods`
226+
227+
.. _laravel-list-fields-command:
228+
229+
Run a Shell Command
230+
~~~~~~~~~~~~~~~~~~~
231+
232+
You can see a list of fields in a collection by running the following
233+
command in your shell from your project's root directory:
234+
235+
.. code-block:: bash
236+
237+
php artisan db:table <collection name>
238+
239+
This command outputs each collection field and its corresponding data type
240+
under the ``Column`` header. For more information about the ``db:table``
241+
command, see `Laravel: New DB Commands <https://blog.laravel.com/laravel-new-db-commands-and-more>`__
242+
on the official Laravel blog.
243+
244+
.. _laravel-list-fields-methods:
245+
246+
Call Schema Methods
247+
~~~~~~~~~~~~~~~~~~~
248+
249+
You can list the fields in a collection by calling the ``Schema::getColumns()``
250+
schema builder method in your application.
251+
252+
You can also use the following methods to return more information about the
253+
collection fields:
254+
255+
- ``Schema::hasColumn(string $<collection>, string $<field name>)``: checks if the specified field exists
256+
in at least one document
257+
- ``Schema::hasColumns(string $<collection>, string[] $<field names>)``: checks if each specified field exists
258+
in at least one document
259+
260+
.. note::
261+
262+
MongoDB is a schemaless database, so the preceding methods query the collection
263+
data rather than the database schema. If the specified collection doesn't exist
264+
or is empty, these methods return a value of ``false``.
265+
266+
Example
267+
```````
268+
269+
The following example passes a collection name to the ``Schema::getColumns()``
270+
method to retrieve each field in the ``flowers`` collection:
271+
272+
.. code-block:: php
273+
274+
$fields = Schema::getColumns('flowers');
275+
175276
Create and Drop Collections
176277
---------------------------
177278

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /