You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 10, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+29-28Lines changed: 29 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,15 @@
1
1
# How To Build a Laravel + MongoDB Back End Service
2
2
3
-
This code was writting to accompany [this article](https://www.mongodb.com/developer/languages/php/laravel-mongodb-tutorial/?utm_campaign=devrel).
3
+
This code was written to accompany [this tutorial article](https://www.mongodb.com/developer/languages/php/laravel-mongodb-tutorial/?utm_campaign=devrel).
4
4
5
5
6
6
## Prerequisites
7
7
You'll need the following installed on your computer to follow along with this tutorial:
8
8
9
9
- A MongoDB Atlas cluster
10
-
-[Create a free cluster](https://www.mongodb.com/try?utm_campaign=devrel) and [load the MongoDB sample data](https://www.mongodb.com/basics/sample-database?utm_campaign=devrel).
11
-
- A code editor of your choice.
10
+
-[Create a **free** cluster](https://www.mongodb.com/try?utm_campaign=devrel) and [load the MongoDB sample data](https://www.mongodb.com/basics/sample-database?utm_campaign=devrel).
11
+
- A GitHub account if you want to use GitHub Codespaces (a 1-click experience)
12
+
- A code editor of your choice for local development
12
13
- We suggeest [Visual Studio Code](https://code.visualstudio.com/download). Check the optional [MongoDB for VS Code](https://www.mongodb.com/products/vs-code?utm_campaign=devrel) extension.
13
14
14
15
The article mentions several ways to get a Laravel development environment up and running.
@@ -29,45 +30,45 @@ The article mentions several ways to get a Laravel development environment up an
29
30
30
31
# 👋 Before you run this Laravel app
31
32
32
-
## 1. Laravel setup
33
+
## 1. Final Laravel app setup
33
34
34
-
After cloning the code repo or launching a Docker/CodeSpaces instance, the project needs a few more things before the Laravel App can run:
35
+
After cloning the code repo or launching a Docker/CodeSpaces instance, a script called `init_repo.sh` will be automatically executed (as setup in devcontainer.json) to:
35
36
36
-
1. install dependencies via Composer
37
-
2. create a new .env file
38
-
3. generate a new Laravel App Key
37
+
- install dependencies via Composer
38
+
- create a new .env file
39
+
- generate a new Laravel App Key
39
40
40
-
You can conveniently do all three by running the `init_repo.sh` from the
41
-
PHP container.
41
+
1. All you need to do is to **add your MongoDB credentials in Laravel's .env file**, using the MONGODB_URI environment variable. Here's [how to get your credentials](https://www.mongodb.com/docs/guides/atlas/connection-string/?utm_campaign=devrel) It looks something like this:
The script will install the package via Composer, create an .env and create an App key.
47
+
❗Note that this branch already has the Laravel Model and Migrations already created and ready, but the tables have been initialized yet.
46
48
47
-
<imgsrc="https://i.imgur.com/6P6MlFX.png">
49
+
2. You can test your credentials by using the code's API endpoint
48
50
49
-
## 2. Ready!
51
+
```
52
+
<siteroot>/api/ping/
53
+
```
50
54
51
-
Head to the siteURL and you should see the Laravel Homepage
55
+
Find the site's root URL by going to the "Ports" tab and click on the globe icon of port 80
52
56
53
57
<imgsrc="https://i.imgur.com/pkORDBj.png">
54
58
55
-
<p></p>
59
+
3. If the MongoDB ping test worked, use this command in the terminal to initialize the tables
56
60
57
-
<imgsrc="https://i.imgur.com/fbZlygD.png">
58
-
59
-
Our base Laravel app is ready 🥳.
60
-
61
-
❗Note that this branch already has the Model and Migrations already setup.
61
+
`php artisan migrate:refresh`
62
62
63
+
<p></p>
63
64
64
-
**Next**, you can connect to a MongoDB Cluster and try some of the things we talked about in our [How To Build a Laravel + MongoDB Back End Service](https://www.mongodb.com/developer/languages/php/laravel-mongodb-tutorial/)
65
+
## 2. Ready!
65
66
66
-
Add the MongoDB connection string with the username / password to the .env file. add this line, but remember that your connection string might look different.
**Next**, try some of the things we talked about in our [How To Build a Laravel + MongoDB Back End Service](https://www.mongodb.com/developer/languages/php/laravel-mongodb-tutorial/)
71
72
72
73
# 🚀 Launch locally with Docker
73
74
@@ -76,10 +77,10 @@ Assuming that you already have Docker Desktop installed on Windows/Mac or Docker
76
77
- clone the repository to a local directory
77
78
- navigate to the ./devcontainer folder
78
79
- execute `docker compose up`
79
-
- in the PHP container, execute the init_repo.sh script
80
-
81
-
Once the containe(s) are up, visit http://localhost
80
+
- in the PHP container, execute `sh init_repo.sh`
81
+
- initialize your .env file as instructed above
82
82
83
+
Once the container(s) are up, visit http://localhost
Copy file name to clipboardExpand all lines: src/.env.example
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -10,8 +10,8 @@ LOG_CHANNEL=stack
10
10
LOG_DEPRECATIONS_CHANNEL=null
11
11
LOG_LEVEL=debug
12
12
13
-
DB_CONNECTION=mysql# this 'mysql' is the Laravel connection name
14
-
DB_HOST=mysql# this 'mysql' refers to the Docker environment's 'mysql' service in lieu of the IP. It is defined in the docker-compose.yml file
13
+
DB_CONNECTION=mysql#'mysql' is the Laravel connection name defined in config/database/php
14
+
DB_HOST=mysql_service# 'mysql_service' refers to the Docker environment's 'mysql_service' container in lieu of the IP. It is defined in the docker-compose.yml file
15
15
DB_PORT=3306
16
16
DB_DATABASE=mydatabase# arbitrary database name for this project
17
17
DB_USERNAME=root# our docker MySQL root username and password, defined in docker-compose.yml
0 commit comments