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

This repository illustrates a tutorial article about how to use MongoDB with Laravel 9 and the Laravel MongoDB package

Notifications You must be signed in to change notification settings

mongodb-developer/laravel-mongodb-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How To Build a Laravel + MongoDB Back End Service

This code was writting to accompany this article.

Prerequisites

You'll need the following installed on your computer to follow along with this tutorial:

The article mentions several ways to get a Laravel development environment up and running.

🚀 Launch this repo in CodeSpaces

⏳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

👋 Before you run this Laravel app

1. Laravel setup

After cloning the code repo or launching a Docker/CodeSpaces instance, the project needs a few more things before the Laravel App can run:

  1. install dependencies via Composer
  2. create a new .env file
  3. generate a new Laravel App Key

You can conveniently do all three by running the init_repo.sh from the PHP container.

The script will install the package via Composer, create an .env and create an App key.

2. Ready!

Head to the site URL and you should see the Laravel Homepage

Our base Laravel app is ready 🥳.

❗Note that this branch already has the Model and Migrations already setup.

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

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.

In .env, add

DB_URI=mongodb+srv://USERNAME:PASSWORD@clustername.subdomain.mongodb.net/?retryWrites=true&w=majority

🚀 Launch locally with Docker

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 the init_repo.sh script

Once the containe(s) are up, visit http://localhost

Optional: Xdebug

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.

CodeSpaces and (inside a Devcontainer)

{
  "name": "Listen for Xdebug",
  "type": "php",
  "request": "launch",
  "port": 9003,
  "pathMappings":  {
    "/var/www/htdoc": "${workspaceFolder}"
  }
},

local development with Docker

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"
  }
},

Disclaimer

Use at your own risk; not a supported MongoDB product

About

This repository illustrates a tutorial article about how to use MongoDB with Laravel 9 and the Laravel MongoDB package

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •