@@ -10,19 +10,20 @@ Tutorial: Build a Back End Service by Using {+odm-long+}
10
10
11
11
.. meta::
12
12
:keywords: php framework, odm, code example, crud
13
- :description: Learn how to set up a back end and perform CRUD operations by using Laravel MongoDB .
13
+ :description: Learn how to set up a back end and perform CRUD operations by using Laravel MongoDB.
14
14
15
15
.. contents:: On this page
16
16
:local:
17
17
:backlinks: none
18
- :depth: 1
18
+ :depth: 2
19
19
:class: singlecol
20
20
21
21
Overview
22
22
--------
23
23
24
24
In this tutorial, you create a simple REST back end for a front-end app
25
- by using {+odm-long+}. The tutorial uses Laravel's built-in API routing.
25
+ by using {+odm-long+}. The tutorial uses Laravel's built-in API routing
26
+ features.
26
27
27
28
Prerequisites
28
29
-------------
73
74
74
75
php artisan serve
75
76
76
- Navigate to http://127.0.0.1:8000/info to view the PHPinfo page.
77
- Verify that the {+php-extension+} is installed.
77
+ After the application begins running, navigate to
78
+ http://127.0.0.1:8000/info to view the PHPinfo page. Scroll down
79
+ to or search for the **mongodb** entry to verify that
80
+ the {+php-extension+} is installed.
78
81
79
82
Run the following command in your shell to install {+odm-long+}:
80
83
@@ -85,20 +88,21 @@ Steps
85
88
.. step:: Configure your MongoDB connection.
86
89
87
90
Open your project's ``config/database.php`` file and update the
88
- ``connection `` array as shown in the following code:
91
+ ``connections `` array as shown in the following code:
89
92
90
93
.. code-block:: php
91
94
92
95
'connections' => [
93
96
'mongodb' => [
94
97
'driver' => 'mongodb',
95
- 'dsn' => env('MONGODB_URI'),
96
- 'database' => '<database name> ',
98
+ 'dsn' => env('MONGODB_URI', '<connection string>' ),
99
+ 'database' => 'db ',
97
100
],
98
101
99
- Ensure that you set the ``MONGODB_URI`` environment variable to
102
+ Ensure that you either replace the connection string placeholder
103
+ in the preceding code or set the ``MONGODB_URI`` environment variable to
100
104
your connection string before you run your application. To learn
101
- more about locating your connection string, see
105
+ how to locate your connection string, see
102
106
:ref:`laravel-quick-start-connection-string` in the Quick Start
103
107
guide.
104
108
@@ -107,9 +111,10 @@ Steps
107
111
108
112
.. code-block:: php
109
113
110
- 'default' => env('DB_CONNECTION', ' mongodb') ,
114
+ 'default' => ' mongodb',
111
115
112
- The Laravel application can now connect to a MongoDB database.
116
+ The Laravel application can now connect to the ``db`` database in
117
+ your MongoDB cluster.
113
118
114
119
.. step:: Create an endpoint to ping your deployment.
115
120
@@ -138,23 +143,27 @@ Steps
138
143
return ['msg' => $msg];
139
144
});
140
145
141
- Verify that http://127.0.0.1:8000/api/ping shows the succesful
142
- ping message.
146
+ Reload the application, then verify that
147
+ http://127.0.0.1:8000/api/ping shows the succesful ping message.
143
148
144
149
.. step:: Create Eloquent models.
145
150
146
151
Laravel is integrated with Eloquent, an ORM that abstracts the
147
- database backend so that you can connect to different databases by
152
+ database back end so that you can connect to different databases by
148
153
using a common interface.
149
154
150
155
Eloquent provides a ``Model`` class to serve as the interface
151
156
between your code and a specific collection. Instances of the
152
157
``Model`` classes represent rows of tables in relational
153
158
databases. In MongoDB, they are documents in the collection.
154
159
155
- Your models can define fillable fields if you want to enforce a
156
- document schema in your application and prevent errors such as name
157
- typos.
160
+ .. tip::
161
+
162
+ You can define fillable fields in your Eloquent models
163
+ to enforce a document schema in your application and prevent
164
+ errors such as name typos. To learn more, see the
165
+ :ref:`laravel-model-mass-assignment` section of the Eloquent
166
+ Model Class guide.
158
167
159
168
Create an Eloquent model called ``CustomerMongoDB`` by running
160
169
the following command from the project root:
@@ -244,8 +253,8 @@ Steps
244
253
the ``embedsMany()`` and ``embedsOne()`` methods.
245
254
246
255
As shown in the preceding step, you can define top-level schema
247
- attributes. However, it is more complicated when using arrays
248
- and embedded documents.
256
+ attributes. However, it is more complicated when to define these
257
+ attribute if your documents include arrays and embedded documents.
249
258
250
259
You can create the model's data structures in PHP. In the
251
260
following example, the ``address`` field is an object type.
@@ -461,13 +470,13 @@ Conclusion
461
470
462
471
In this tutorial, you learned how to create a back-end service by using
463
472
Laravel and MongoDB for a front-end web application.
464
- This tutorial also showed why the document model leads to higher
473
+ This tutorial also showed how you can use the document model to improve
465
474
database efficiency and scalability. You can use the document model with the
466
475
MongoDB Query API to create better apps with less downtime.
467
476
468
477
You can access the full code for this tutorial in the
469
478
:github:`laravel-mongodb-tutorial
470
479
</mongodb-developer/laravel-mongodb-tutorial/>` repository on GitHub.
471
480
472
- See the :ref:`laravel_fundamentals` guides to learn more about
473
- {+odm-long+}'s features.
481
+ Navigate through the rest of the :ref:`laravel-docs-landing`
482
+ documentation to learn more about {+odm-long+}'s features.
0 commit comments