Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Commit 0651ac5

Browse files
committed
Update documentation and .env.example
1 parent 00cb759 commit 0651ac5

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

.devcontainer/docker-compose.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,24 @@ services:
5151
# MySQL Service
5252
mysql_service:
5353
image: ${MYSQL_IMAGE}
54+
5455
environment:
5556
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
5657
MYSQL_DATABASE: ${MYSQL_DATABASE}
5758

5859
# the two variales below don't seem to be used at all. Commenting out for now
5960
#MYSQL_USER: myuser
6061
#MYSQL_PASSWORD: mypassword
62+
63+
# do NOT try mounting a volume in CodeSpaces as it will fail and MySQL will not launch (try pinging mysql_service)
64+
# this is only useful when working locally on a website with a non-ephemeral database
6165
#volumes:
6266
# map local /data/ folder to container /var/lib/mysql for MySQL data persistence
6367
# - ${MYSQL_DATA_HOST_PATH}:${MYSQL_DATA_CONTAINER_PATH}
68+
6469
ports:
6570
- "3306:3306"
66-
# host_port:container_port
71+
# syntax = host_port:container_port
6772

6873

6974
# redis:

README.md

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# How To Build a Laravel + MongoDB Back End Service
22

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).
44

55

66
## Prerequisites
77
You'll need the following installed on your computer to follow along with this tutorial:
88

99
- 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
1213
- 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.
1314

1415
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
2930

3031
# 👋 Before you run this Laravel app
3132

32-
## 1. Laravel setup
33+
## 1. Final Laravel app setup
3334

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:
3536

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
3940

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:
4242

43-
<img src="https://i.imgur.com/P0ZNlot.png">
43+
```
44+
MONGODB_URI=mongodb+srv://USERNAME:PASSWORD@clustername.subdomain.mongodb.net/?retryWrites=true&w=majority
45+
```
4446

45-
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.
4648

47-
<img src="https://i.imgur.com/6P6MlFX.png">
49+
2. You can test your credentials by using the code's API endpoint
4850

49-
## 2. Ready!
51+
```
52+
<siteroot>/api/ping/
53+
```
5054

51-
Head to the site URL 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
5256

5357
<img src="https://i.imgur.com/pkORDBj.png">
5458

55-
<p></p>
59+
3. If the MongoDB ping test worked, use this command in the terminal to initialize the tables
5660

57-
<img src="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`
6262

63+
<p></p>
6364

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!
6566

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.
67+
<img src="https://i.imgur.com/fbZlygD.png">
6768

68-
In .env, add
69+
Our base Laravel app is ready 🥳.
6970

70-
`DB_URI=mongodb+srv://USERNAME:PASSWORD@clustername.subdomain.mongodb.net/?retryWrites=true&w=majority`
71+
**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/)
7172

7273
# 🚀 Launch locally with Docker
7374

@@ -76,10 +77,10 @@ Assuming that you already have Docker Desktop installed on Windows/Mac or Docker
7677
- clone the repository to a local directory
7778
- navigate to the ./devcontainer folder
7879
- 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
8282

83+
Once the container(s) are up, visit http://localhost
8384

8485
# Optional: Xdebug
8586

src/.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LOG_CHANNEL=stack
1010
LOG_DEPRECATIONS_CHANNEL=null
1111
LOG_LEVEL=debug
1212

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
1515
DB_PORT=3306
1616
DB_DATABASE=mydatabase # arbitrary database name for this project
1717
DB_USERNAME=root # our docker MySQL root username and password, defined in docker-compose.yml

0 commit comments

Comments
 (0)