Skip to content

Merge 4.7 into 4.8 #3101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 106 additions & 5 deletions docs/fundamentals/database-collection.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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
Loading