Getting Started with Laravel 5.4 and MongoDB

Create a MongoDB Laravel facade for easy access and configuration

I use and love MongoDB for many years now. In the early days in a Java environment but since 2013 — when I discovered Laravel — mostly together with PHP.

Working with MongoDB requires a different thinking and concept compared to traditional relational database development. There are some PHP/MongoDB libraries out there but I recommend to directly use the excellent MongoDB PHP Library.

This tutorial describes the steps to create a MongoDB Laravel Facade to access the database like this:

UPDATE: I created a MongoDB package that uses the package auto-discovery feature introduced in Laravel 5.5.

Install Laravel and MongoDB database

Follow the Laravel installation guide and install MongoDB. On OS X I highly recommend to use Homebrew. In addition install Laravel Valet — a minimalistic development environment using Nginx and Dnsmasq. I don’t want to work without it anymore.

To test the successful MongoDB installation open the terminal, connect to MongoDB, create a database and collection and insert a document:

Create a New Project

Open the terminal, change to your Valet directory and type:

This creates a new Laravel project and adds the MongoDB PHP Library to it. The application can be viewed in the browser via http://myproject.dev

To test the MongoDB connection open routes/web.php file and add:

http://myproject.dev/mongo returns the JSON document added before.

Service, ServiceProvider, Facade

Create an app/Mongo directory, put the 3 files (Service, ServiceProvider, Facade) there, modify the configuration in config/app.php, config/services.php, and .env. That’s it!

Mongo directory structure within a Laravel project

Service.php:

ServiceProvider.php:

Facade.php:

Then modify the configuration:

config/app.php:

config/services.php:

.env:

Now let’s use the Mongo Facade in your application. Therefore just modify the routes/web.php from before:

http://myproject.dev/mongo returns the JSON document like before.

Final Thoughts

Why didn’t you create a Laravel package?
I had one. But over time I added features and even more features to it. At the end I was not satisfied with the package and even did not use it anymore. For my new projects I started to use the bare minimum as described here. But maybe I create a package again in the future.

Why do you program in PHP, and not JavaScript, Ruby, Python or Java?
I learned Java before JDK 1.0 and it was my primary programming language over years. Beside I did some Wordpress programming, JavaScript (JQuery, Angular 1) and Ruby, but Java was definitely my main language.

By chance I discovered Laravel in late 2013, I bought the eBook “Laravel: From Apprentice to Artisan” and thought “wow”, you can create quality code in PHP as well! Since then I used Laravel with MongoDB for most of my projects.

What I really like about Laravel is the community, the excellent tutorials at Laracasts, and the brilliant ecosystem with Forge, Envoyer, Valet, Passport, … Awesome job!

Why do you often prefer MongoDB over MySQL?
It really depends on the project. But I realized that for API heavy web applications MongoDB is often a good choice.

But there are 3 features that stand out (my personal opinion):

  1. Native JSON support
  2. Scalability with sharding and replica sets
  3. GridFS for storing binary data like image uploads

I ❤️ Shopify & Next.js. checkout-agency.com, Founder

I ❤️ Shopify & Next.js. checkout-agency.com, Founder