Skip to content

Commit 318afdf

Browse files
committed
DOCSP-41557: v4.7 commands and methods
1 parent 58682e1 commit 318afdf

File tree

1 file changed

+93
-5
lines changed

1 file changed

+93
-5
lines changed

docs/fundamentals/database-collection.txt

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,105 @@ 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+
This section describes how to see information about each collection in a database
165+
in the following ways:
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 shell
176+
command from your project root:
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+
186+
.. _laravel-list-coll-methods:
187+
188+
Call Database or Schema Methods
189+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190+
191+
You can list the collections in a database by calling the following
192+
methods in your application:
193+
194+
- ``DB::listCollections()``: lists information about each collection by
195+
using the query builder
196+
- ``Schema::getTablesListing()``: lists the name of each collection by
197+
using the schema builder
198+
- ``Schema::getTables()``: lists the name and size of each collection by
199+
using the schema builder
200+
201+
Example
202+
```````
203+
204+
The following example accesses a database connection, then calls the
205+
``listCollections()`` query builder method to retrieve information about
206+
the collections in the database:
170207

171208
.. code-block:: php
172209

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

212+
List Collection Fields
213+
----------------------
214+
215+
This section describes how to see information about each field in a collection
216+
in the following ways:
217+
218+
- :ref:`laravel-list-fields-command`
219+
- :ref:`laravel-list-fields-methods`
220+
221+
.. _laravel-list-fields-command:
222+
223+
Run a Shell Command
224+
~~~~~~~~~~~~~~~~~~~
225+
226+
You can see a list of fields in a collection by running the following shell
227+
command from your project root:
228+
229+
.. code-block:: bash
230+
231+
php artisan db:table <collection name>
232+
233+
This command outputs each collection field and its corresponding data type
234+
under the ``Column`` header. For more information about the ``db:table``
235+
command, see `Laravel: New DB Commands <https://blog.laravel.com/laravel-new-db-commands-and-more>`__.
236+
237+
.. _laravel-list-fields-methods:
238+
239+
Call Schema Methods
240+
~~~~~~~~~~~~~~~~~~~
241+
242+
You can list the collections in a database by calling the ``Schema::getColumns()``
243+
schema builder method in your application.
244+
245+
You can also call the following methods to return more information about the
246+
collection fields:
247+
248+
- ``Schema::hasColumn(string $field)``: checks if the specified field exists
249+
in at least one document
250+
- ``Schema::hasColumns(string[] $fields)``: checks if each specified field exists
251+
in at least one document
252+
253+
Example
254+
```````
255+
256+
The following example passes a collection name to the ``Schema::getColumns()``
257+
method to retrieve each field in the ``flowers`` collection:
258+
259+
.. code-block:: php
260+
261+
$fields = Schema::getColumns('flowers');
262+
175263
Create and Drop Collections
176264
---------------------------
177265

0 commit comments

Comments
 (0)