PHPackages                             leroy-merlin-br/mongolid-laravel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. leroy-merlin-br/mongolid-laravel

ActiveLibrary[Database &amp; ORM](/categories/database)

leroy-merlin-br/mongolid-laravel
================================

Easy, powerful and ultrafast MongoDB ODM for Laravel.

v3.8.1(7mo ago)23827.8k↓100%38[7 issues](https://github.com/leroy-merlin-br/mongolid-laravel/issues)[3 PRs](https://github.com/leroy-merlin-br/mongolid-laravel/pulls)2MITPHPPHP ^8.1CI passing

Since Jun 3Pushed 2mo ago36 watchersCompare

[ Source](https://github.com/leroy-merlin-br/mongolid-laravel)[ Packagist](https://packagist.org/packages/leroy-merlin-br/mongolid-laravel)[ RSS](/packages/leroy-merlin-br-mongolid-laravel/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (51)Used By (2)

[![Build Status](https://github.com/leroy-merlin-br/mongolid-laravel/workflows/Tests/badge.svg)](https://github.com/leroy-merlin-br/mongolid-laravel/actions?query=workflow%3ATests)[![Coverage Status](https://camo.githubusercontent.com/af778eab7c9e1f43deb7a68d24f62685b75d7ee7f40339b524462f6bb824cb09/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f436f7665726167652f3165643432636562386639393436323762393838376139376432393664313166)](https://www.codacy.com/gh/leroy-merlin-br/mongolid-laravel/dashboard?utm_source=github.com&utm_medium=referral&utm_content=leroy-merlin-br/mongolid-laravel&utm_campaign=Badge_Coverage)[![Latest Stable Version](https://camo.githubusercontent.com/0abfce34d44919fecdde0f7f2bdf3faf07006812c2a4512b59b668046cf6b5c9/68747470733a2f2f706f7365722e707567782e6f72672f6c65726f792d6d65726c696e2d62722f6d6f6e676f6c69642d6c61726176656c2f762f737461626c652e706e67)](https://packagist.org/packages/leroy-merlin-br/mongolid-laravel)[![Monthly Downloads](https://camo.githubusercontent.com/ba21d64d9bbd4835a1c4a08436f369226030b4e410fae56b26ed04eba2d6f151/68747470733a2f2f706f7365722e707567782e6f72672f6c65726f792d6d65726c696e2d62722f6d6f6e676f6c69642d6c61726176656c2f642f6d6f6e74686c792e706e67)](https://packagist.org/packages/leroy-merlin-br/mongolid-laravel)[![Latest Unstable Version](https://camo.githubusercontent.com/0d54de77779ced63d5b9593eadcc314d080b63bb40e7e65e7a65a99fb83732dd/68747470733a2f2f706f7365722e707567782e6f72672f6c65726f792d6d65726c696e2d62722f6d6f6e676f6c69642d6c61726176656c2f762f756e737461626c652e706e67)](https://packagist.org/packages/leroy-merlin-br/mongolid-laravel)[![License](https://camo.githubusercontent.com/5c16a7679c6665a580d96e60a74b55879af30558c0410ff0e4748855ac45c77e/68747470733a2f2f706f7365722e707567782e6f72672f6c65726f792d6d65726c696e2d62722f6d6f6e676f6c69642d6c61726176656c2f6c6963656e73652e706e67)](https://packagist.org/packages/leroy-merlin-br/mongolid-laravel)

[![MongoLid](https://user-images.githubusercontent.com/1991286/28967747-fe5c258a-78f2-11e7-91c7-8850ffb32004.png)](https://user-images.githubusercontent.com/1991286/28967747-fe5c258a-78f2-11e7-91c7-8850ffb32004.png)

MongoLid (Laravel Package)
==========================

[](#mongolid-laravel-package)

- [Introduction](#introduction)
- [Installation](#installation)
- [Basic Usage](#basic-usage)
- [Authentication](#authentication)
- [Troubleshooting](#troubleshooting)
- [License](#license)
- [Additional Information](#additional_information)

Introduction
------------

[](#introduction)

MongoLid ODM (Object Document Mapper) provides a beautiful, simple implementation for working with MongoDB. Each database collection can have a corresponding "Model" which is used to interact with that collection.

> Note: The ODM implementation is within the [(non laravel) mongolid repository](https://github.com/leroy-merlin-br/mongolid).

Installation
------------

[](#installation)

Install with `composer require` (use one of the above tags if needed).

```
composer require leroy-merlin-br/mongolid-laravel
```

**Note:** Mongolid Laravel 2.0 only supports Laravel 5.4+. For older versions use the tags:

- Laravel 4.2 `"leroy-merlin-br/mongolid-laravel": "^0.7"`
- Laravel 5.1 `"leroy-merlin-br/mongolid-laravel": "2.0.0-beta4"`
- Laravel 5.2 `"leroy-merlin-br/mongolid-laravel": "2.0.0-beta6"`
- Laravel 5.3 `"leroy-merlin-br/mongolid-laravel": "2.0.0-beta6"`

Make sure to set minimum stability to `dev` when using a beta tag (`composer config minimum-stability dev`).

> **Note**: If you are using Laravel 5.5, the next steps for providers and aliases are unnecessaries. MongoLid supports Laravel new [Package Discovery](https://laravel.com/docs/5.5/packages#package-discovery).

In your `config/app.php` add `'MongolidLaravel\MongolidServiceProvider'` to the end of the `$providers` array

```
'providers' => [
    Illuminate\Translation\TranslationServiceProvider::class,
    Illuminate\Validation\ValidationServiceProvider::class,
    Illuminate\View\ViewServiceProvider::class,
    ...
    MongolidLaravel\MongolidServiceProvider::class,
],
```

(**Optional**) At the end of `config/app.php` add `'MongoLid'    => 'MongolidLaravel\MongoLidModel'` to the `$aliases` array

```
'aliases' => [
    'App'         => Illuminate\Support\Facades\App::class,
    'Artisan'     => Illuminate\Support\Facades\Artisan::class,
    ...
    'MongoLid'    => MongolidLaravel\MongoLidModel::class,
],
```

Lastly, be sure to configure a database connection in `config/database.php`:

Paste the settings bellow at the end of your `config/database.php`, before the last `];`:

**Notice:** It must be **outside** of `connections` array.

```
/*
|--------------------------------------------------------------------------
| MongoDB Databases
|--------------------------------------------------------------------------
|
| MongoDB is a document database with the scalability and flexibility
| that you want with the querying and indexing that you need.
| Mongolid Laravel use this config to starting querying right now.
|
*/

'mongodb' => [
    'default' => [
        'host'     => env('DB_HOST', '127.0.0.1'),
        'port'     => env('DB_PORT_NUMBER', 27017),
        'database' => env('DB_DATABASE', 'my_database'),
        'username' => env('DB_USERNAME', null),
        'password' => env('DB_PASSWORD', null),
    ],
],
```

For cluster with automatic failover, you need to set `cluster` key containing all `nodes` along with `replica_set` name.

```
'mongodb' => [
    'default' => [
        'cluster' => [
            'replica_set' => env('DB_REPLICA_SET', null),
            'nodes' => [
                'primary' => [
                    'host' => env('DB_HOST_A', 'host-a'),
                    'port' => env('DB_PORT_A', 27017),
                ],
                'secondary' => [
                    'host' => env('DB_HOST_B', 'host-b'),
                    'port' => env('DB_PORT_B', 27017),
                ],
            ],
        ],
        'database' => env('DB_DATABASE', 'mongolid'),
        'username' => env('DB_USERNAME', null),
        'password' => env('DB_PASSWORD', null),
    ],
],
```

You can configure as much nodes as needed, node names (e.g. `primary` and `secondary` ) are optional.

> **Note:** If you don't specify the `mongodb` key in your `config/database.php` MongoLid will automatically try to connect to '127.0.0.1:27017' and use a database named 'mongolid'.

You may optionally provide a `connection_string` key to set a fully-assembled connection string that will override all other connection options. More info about connection string are found in [MongoDB documentation](https://docs.mongodb.com/manual/reference/connection-string/).

```
'mongodb' => [
    'default' => [
        'connection_string' => 'mongodb://host-a:27017,host-b:27917/mongolid?replicaSet=rs-ds123',
    ],
],
```

Also, it is possible to pass `options` and `driver_options` to MongoDB Client. Mongolid always overrides `typeMap` configuration of `driver_options` to `array` because it makes easier to use internally with models. Possible `options` and `driver_options` are present on [`MongoDB\Client` documentation](https://docs.mongodb.com/php-library/master/reference/method/MongoDBClient__construct/).

Basic Usage
-----------

[](#basic-usage)

To get started, create an MongoLid model. Models typically live in the `app/models` directory, but you are free to place them anywhere that can be auto-loaded according to your `composer.json` file.

**Defining a MongoLid Model**

```
