This code was written to accompany this tutorial article.
You'll need the following installed on your computer to follow along with this tutorial:
- A MongoDB Atlas cluster
- A GitHub account if you want to use GitHub Codespaces (a 1-click experience)
- The
master
repo has all the code, but we also have astarter
branch if you want to follow the project from scratch and build the migrations etc.
- The
- A code editor of your choice for local development
- We suggeest Visual Studio Code. Check the optional MongoDB for VS Code extension.
The article mentions several ways to get a Laravel development environment up and running.
⏳Codespaces will build the app's container(s). This may take ~3 minutes.
✅Done! We now have our project running inside CodeSpaces. We can proceed to setting up Laravel
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:
- install dependencies via Composer
- create a new .env file
- generate a new Laravel App Key
- 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 It looks something like this:
MONGODB_URI=mongodb+srv://USERNAME:[email protected]/?retryWrites=true&w=majority
❗Note that this branch already has the Laravel Model and Migrations already created and ready, but the tables have been initialized yet.
- You can test your credentials by using the code's API endpoint
<siteroot>/api/test_mongodb/
Find the site's root URL by going to the "Ports" tab and click on the globe icon of port 80
- If the MongoDB ping test worked, use this command in the terminal to initialize the tables
php artisan migrate:refresh
Our base Laravel app is ready 🥳.
Next, try some of the things we talked about in our How To Build a Laravel + MongoDB Back End Service
Assuming that you already have Docker Desktop installed on Windows/Mac or Docker on Linux,
- clone the repository to a local directory
- navigate to the ./devcontainer folder
- execute
docker compose up
- in the PHP container, execute
sh init_repo.sh
- initialize your .env file as instructed above
Once the container(s) are up, visit http://localhost
The xdebug.php-debug VS Code extension is automatically installed if you launch via devcontainer.json.
👀 Important: our .devcontainer/.docker/php/xdebug.ini
file is setup by default with xdebug.client_host=localhost
, which should works for CodeSpaces and Devcontainers.
For local development, you need to replace localhost
with the IP where your code IDE runs or a dns name that maps to it. That's because your PHP container and the IDE host tend to have different IPs.
If you are using our container directly (docker compose up), or via VS Code (devcontainer), we suggest the following Xdebug configs visual studio. Note the difference in path mapping.
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/var/www/htdoc": "${workspaceFolder}"
}
},
The debug config file is located in <repository_dir>/.vscode/launch.json
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/var/www/htdoc": "${workspaceFolder}/src"
}
},
Use at your own risk; not a supported MongoDB product