Skip to content

Commit

Permalink
DOCSP-41557: New v4.7 commands and methods (#3084)
Browse files Browse the repository at this point in the history
* DOCSP-41557: v4.7 commands and methods

* reword, fixes

* RR feedback

* JT review

* wording

* fix
  • Loading branch information
norareidy authored Aug 16, 2024
1 parent 185d93a commit f35d632
Showing 1 changed file with 106 additions and 5 deletions.
111 changes: 106 additions & 5 deletions docs/fundamentals/database-collection.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,118 @@ as in the preceding example, but the query is constructed by using the
List Collections
----------------

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

The following example accesses a database connection, then
calls the ``listCollections()`` method to retrieve information about the
collections in the database:
- :ref:`laravel-list-coll-command`
- :ref:`laravel-list-coll-methods`

.. _laravel-list-coll-command:

Run a Shell Command
~~~~~~~~~~~~~~~~~~~

You can list the collections in a database by running the following
command in your shell from your project's root directory:

.. code-block:: bash

php artisan db:show

This command outputs information about the configured database and lists its
collections under the ``Table`` header. For more information about the ``db:show``
command, see `Laravel: New DB Commands <https://blog.laravel.com/laravel-new-db-commands-and-more>`__
on the official Laravel blog.

.. _laravel-list-coll-methods:

Call Database or Schema Methods
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can list the collections in a database by calling the following
methods in your application:

- ``DB::listCollections()``: lists information about each collection by
using the query builder
- ``Schema::getTablesListing()``: lists the name of each collection by
using the schema builder
- ``Schema::getTables()``: lists the name and size of each collection by
using the schema builder

.. note::

MongoDB is a schemaless database, so the preceding schema builder methods
query the database data rather than the schema.

Example
```````

The following example accesses a database connection, then calls the
``listCollections()`` query builder method to retrieve information about
the collections in the database:

.. code-block:: php

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

List Collection Fields
----------------------

You can take either of the following actions to see information
about each field in a collection:

- :ref:`laravel-list-fields-command`
- :ref:`laravel-list-fields-methods`

.. _laravel-list-fields-command:

Run a Shell Command
~~~~~~~~~~~~~~~~~~~

You can see a list of fields in a collection by running the following
command in your shell from your project's root directory:

.. code-block:: bash

php artisan db:table <collection name>

This command outputs each collection field and its corresponding data type
under the ``Column`` header. For more information about the ``db:table``
command, see `Laravel: New DB Commands <https://blog.laravel.com/laravel-new-db-commands-and-more>`__
on the official Laravel blog.

.. _laravel-list-fields-methods:

Call Schema Methods
~~~~~~~~~~~~~~~~~~~

You can list the fields in a collection by calling the ``Schema::getColumns()``
schema builder method in your application.

You can also use the following methods to return more information about the
collection fields:

- ``Schema::hasColumn(string $<collection>, string $<field name>)``: checks if the specified field exists
in at least one document
- ``Schema::hasColumns(string $<collection>, string[] $<field names>)``: checks if each specified field exists
in at least one document

.. note::

MongoDB is a schemaless database, so the preceding methods query the collection
data rather than the database schema. If the specified collection doesn't exist
or is empty, these methods return a value of ``false``.

Example
```````

The following example passes a collection name to the ``Schema::getColumns()``
method to retrieve each field in the ``flowers`` collection:

.. code-block:: php

$fields = Schema::getColumns('flowers');

Create and Drop Collections
---------------------------

Expand Down

0 comments on commit f35d632

Please sign in to comment.