Skip to content

Commit 0126d53

Browse files
author
Chris Cho
committed
quick start updates
1 parent f4487c9 commit 0126d53

File tree

7 files changed

+316
-69
lines changed

7 files changed

+316
-69
lines changed

docs/quick-start.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ database from a command line-based development environment.
2626

2727
.. tip::
2828

29-
If you prefer to use GitHub Codespaces or Docker as your development
30-
environment, see the linked code repository in the
29+
If you prefer to set up your development environment in GitHub Codespaces
30+
or Docker, see the linked code repository in the
3131
`How to Build a Laravel + MongoDB Back End Service <https://www.mongodb.com/developer/languages/php/laravel-mongodb-tutorial/>`__
3232
MongoDB Developer Center tutorial.
3333

@@ -53,6 +53,9 @@ that connects to a MongoDB deployment.
5353
/quick-start/download-and-install/
5454
/quick-start/create-a-deployment/
5555
/quick-start/create-a-connection-string/
56-
/quick-start/connect-to-mongodb/
56+
/quick-start/configure-mongodb/
57+
/quick-start/create-a-model-view-controller/
58+
/quick-start/view-data/
59+
/quick-start/write-data/
5760
/quick-start/next-steps/
5861

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.. _laravel-quick-start-connect-to-mongodb:
2+
3+
==================================
4+
Confingure Your MongoDB Connection
5+
==================================
6+
7+
.. facet::
8+
:name: genre
9+
:values: tutorial
10+
11+
.. meta::
12+
:keywords: test connection, runnable, code example
13+
14+
.. procedure::
15+
:style: connected
16+
17+
.. step:: Set the Connection String in the Database Configuration
18+
19+
Navigate to the ``config`` directory from your application root directory.
20+
Open the ``database.php`` file and set the default database connection
21+
to ``mongodb`` as shown in the following line:
22+
23+
.. code-block:: php
24+
25+
'default' => env('DB_CONNECTION', 'mongodb'),
26+
27+
Add the following highlighted ``mongodb`` entry to the ``connections`` array
28+
in the same file. Replace the ``<connection string>`` placeholder with the
29+
connection string that you copied from the :ref:`laravel-quick-start-connection-string`
30+
step of this guide.
31+
32+
.. code-block:: php
33+
:emphasize-lines: 2-6
34+
35+
'connections' => [
36+
'mongodb' => [
37+
'driver' => 'mongodb',
38+
'dsn' => env('DB_URI', '<connection string>'),
39+
'database' => 'sample_mflix',
40+
],
41+
42+
// ...
43+
44+
.. include:: /includes/quick-start/troubleshoot.rst
45+
46+
.. button:: View Sample MongoDB Data
47+
:uri: /quick-start/view-data/

docs/quick-start/connect-to-mongodb.txt

Lines changed: 0 additions & 55 deletions
This file was deleted.

docs/quick-start/download-and-install.txt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@ to a Laravel web application.
5757
.. step:: Install Laravel
5858

5959
Ensure that the version of Laravel you install is compatible with the
60-
version of {+odm-short+}.
60+
version of {+odm-short+}.
61+
62+
63+
Then run the following command to install
64+
Laravel.
6165

6266
.. code-block:: bash
6367

64-
composer global require "laravel/installer"
68+
composer global require laravel/installer
6569

6670
When the installation successfully completes, you should see the
6771
following output:
@@ -107,18 +111,18 @@ to a Laravel web application.
107111

108112
.. code-block:: bash
109113

110-
composer require "mongodb/laravel-mongodb"
114+
composer require mongodb/laravel-mongodb:^{+package-version+}
111115

112116
When the installation successfully completes, you should see the
113-
following in your ``composer.json`` file in the application directory:
117+
following line in the ``require`` object in your ``composer.json`` file:
114118

115119
.. code-block:: json
120+
:copyable: false
121+
122+
"mongodb/laravel-mongodb": "^{+package-version+}"
116123

117-
{
118-
"require": {
119-
"mongodb/laravel-mongodb": "^{+package-version+}"
120-
}
121-
}
124+
After you complete these steps, you should have a new Laravel project
125+
with the {+odm-short+} dependencies installed.
122126

123127
.. include:: /includes/quick-start/troubleshoot.rst
124128

docs/quick-start/next-steps.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ Next Steps
1313

1414
Congratulations on completing the quick start tutorial!
1515

16-
In this tutorial, you created a Laravel web application that performs
17-
read and write database operations on a MongoDB deployment hosted on
16+
After you complete these steps, you have a Laravel web application that
17+
uses the {+odm-long+} to connect to your MongoDB deployment, run a query on
18+
the sample data, and render a retrieved result.
19+
20+
In this tutorial, you created a Laravel web application that uses the
21+
{+odm-long+} to connect to your MongoDB deployment, render the result
22+
of a query, and write data to a MongoDB deployment hosted on
1823
MongoDB Atlas.
1924

2025
Learn more about {+odm-short+} features from the following resources:

docs/quick-start/view-data.txt

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
.. laravel-quick-start-view-data:
2+
3+
========================
4+
View Sample MongoDB Data
5+
========================
6+
7+
.. facet::
8+
:name: genre
9+
:values: tutorial
10+
11+
.. meta::
12+
:keywords: test connection, runnable, code example
13+
14+
Follow the steps in this section to create the
15+
To create a webpage that displays data from your database, you need
16+
to create a model, view, and controller.
17+
18+
.. procedure::
19+
:style: connected
20+
21+
.. step:: Create a Sample Model and Controller
22+
23+
Create a model called ``Movie`` to access data from the sample ``movies``
24+
collection in your MongoDB databse. You can create the model with a
25+
corresponding resource controller by running the following command:
26+
27+
.. code-block:: bash
28+
29+
php artisan make:model Movie -cr
30+
31+
When the command successfully completes, you should see the following
32+
output:
33+
34+
.. code-block:: none
35+
:copyable: false
36+
37+
INFO Model [app/Models/Movie.php] created successfully.
38+
39+
INFO Controller [app/Http/Controllers/MovieController.php] created successfully.
40+
41+
.. step:: Edit the Model to use {+odm-short+}
42+
43+
Navigate to the ``app/Models`` directory and open the ``Movie.php`` file.
44+
Edit the following information in the file:
45+
46+
- Replace the ``Illuminate\Database\Eloquent\Model`` import with ``MongoDB\Laravel\Eloquent\Model``
47+
- Specify ``mongodb`` in the ``$connection`` field
48+
49+
50+
Your ``Movie.php`` file should contain the following code:
51+
52+
.. code-block:: php
53+
54+
<?php
55+
56+
namespace App\Models;
57+
58+
use MongoDB\Laravel\Eloquent\Model;
59+
60+
class Movie extends Model
61+
{
62+
protected $connection = 'mongodb';
63+
}
64+
65+
.. step:: Add a Controller Function
66+
67+
Navigate to the ``app/Http/Controllers`` directory and open the
68+
``MovieController.php`` file. Replace the ``show()`` function
69+
with the following code to retrieve results that match a
70+
database query and render it in the view:
71+
72+
.. code-block:: php
73+
74+
public function show()
75+
{
76+
return view('browse_movies', [
77+
'movies' => Movie::where('runtime', '<', 60)
78+
->where('imdb.rating', '>', 8.5)
79+
->orderBy('imdb.rating', 'desc')
80+
->take(10)
81+
->get()
82+
]);
83+
}
84+
85+
.. step:: Add a Web Route
86+
87+
Navigate to the ``routes`` directory and open the ``web.php`` file.
88+
Add an import for the ``MovieController`` and a route called
89+
``browse_movies`` as shown in the following code:
90+
91+
.. code-block:: php
92+
93+
<?php
94+
95+
// ...
96+
use App\Http\Controllers\MovieController;
97+
98+
Route::get('/browse_movies/', [MovieController::class, 'show']);
99+
100+
.. step:: Generate a View
101+
102+
Navigate to the application root directory and run the following
103+
command to create a view that displays movie data:
104+
105+
.. code-block:: bash
106+
107+
php artisan make:view browse_movies
108+
109+
.. code-block:: none
110+
:copyable: false
111+
112+
INFO View [resources/views/browse_movie.blade.php] created successfully.
113+
114+
Navigate to the ``resources/views`` directory and open the
115+
``browse_movie.blade.php`` file. Replace the contents with the
116+
following code:
117+
118+
.. code-block:: bash
119+
120+
<!DOCTYPE html>
121+
<html>
122+
<head>
123+
<title>Browse Movies</title>
124+
</head>
125+
<body>
126+
<h2>Movies</h2>
127+
128+
@forelse ($movies as $movie)
129+
<p>
130+
Title: {{ $movie->title }}<br>
131+
Year: {{ $movie->year }}<br>
132+
Runtime: {{ $movie->runtime }}<br>
133+
IMDB Rating: {{ $movie->imdb['rating'] }}<br>
134+
IMDB Votes: {{ $movie->imdb['votes'] }}<br>
135+
Plot: {{ $movie->plot }}<br>
136+
</p>
137+
@empty
138+
<p>No results</p>
139+
@endforelse
140+
141+
</body>
142+
</html>
143+
144+
.. step:: Start your Laravel Application
145+
146+
Navigate to the application root directory and run the following command
147+
to start your PHP built-in web server:
148+
149+
.. code-block:: bash
150+
151+
php artisan serve
152+
153+
If the server starts successfully, you should see the following message:
154+
155+
.. code-block: none
156+
157+
INFO Server running on [http://127.0.0.1:8000].
158+
159+
Press Ctrl+C to stop the server
160+
161+
.. step:: View the Movie Data
162+
163+
Open http://127.0.0.1:8000/browse_movies in your web browser. If it runs
164+
successuflly, you should see a list of movies and details about each of them.
165+
166+
.. tip::
167+
168+
You can run the ``php artisan route:list`` command from your application
169+
root directory to view a list of available routes.
170+
171+
.. include:: /includes/quick-start/troubleshoot.rst
172+
173+
.. button:: Write Data
174+
:uri: /quick-start/write-data/

0 commit comments

Comments
 (0)