diff --git a/docs/fundamentals/database-collection.txt b/docs/fundamentals/database-collection.txt index ce67c3946..cc0d3ca70 100644 --- a/docs/fundamentals/database-collection.txt +++ b/docs/fundamentals/database-collection.txt @@ -167,17 +167,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 `__ +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 + +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 `__ +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 $, string $)``: checks if the specified field exists + in at least one document +- ``Schema::hasColumns(string $, string[] $)``: 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 ---------------------------