PHPackages                             neutrino-labs/laravel-mongodb-sync - 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. neutrino-labs/laravel-mongodb-sync

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

neutrino-labs/laravel-mongodb-sync
==================================

Hybrid MySQL-MongoDB synchronization package for Laravel microservices

00PHP

Since Feb 19Pushed 2mo agoCompare

[ Source](https://github.com/neutrino-solucoes-em-tecnologia/laravel-mongodb-sync)[ Packagist](https://packagist.org/packages/neutrino-labs/laravel-mongodb-sync)[ RSS](/packages/neutrino-labs-laravel-mongodb-sync/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel MongoDB Sync
====================

[](#laravel-mongodb-sync)

 [![PHP 8.2+](https://camo.githubusercontent.com/a23fef6d61ed756ea195ca133e7143504dd8c9c97ac94f525dfafe9f5d836cfb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e322b2d3737374242343f7374796c653d666f722d7468652d6261646765266c6f676f3d706870266c6f676f436f6c6f723d7768697465)](https://camo.githubusercontent.com/a23fef6d61ed756ea195ca133e7143504dd8c9c97ac94f525dfafe9f5d836cfb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e322b2d3737374242343f7374796c653d666f722d7468652d6261646765266c6f676f3d706870266c6f676f436f6c6f723d7768697465) [![Laravel 11|12](https://camo.githubusercontent.com/2549b0aefe234f61837a106c6040d7280b7880edf5b5972b0553098f34733117/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d313125323025374325323031322d4646324432303f7374796c653d666f722d7468652d6261646765266c6f676f3d6c61726176656c266c6f676f436f6c6f723d7768697465)](https://camo.githubusercontent.com/2549b0aefe234f61837a106c6040d7280b7880edf5b5972b0553098f34733117/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d313125323025374325323031322d4646324432303f7374796c653d666f722d7468652d6261646765266c6f676f3d6c61726176656c266c6f676f436f6c6f723d7768697465) [![MongoDB 5.x+](https://camo.githubusercontent.com/79d70c74281e72696049b6e64e7f76a97ed265fb21bdb02e2d9533cc5bed0511/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d6f6e676f44422d352e782b2d3437413234383f7374796c653d666f722d7468652d6261646765266c6f676f3d6d6f6e676f6462266c6f676f436f6c6f723d7768697465)](https://camo.githubusercontent.com/79d70c74281e72696049b6e64e7f76a97ed265fb21bdb02e2d9533cc5bed0511/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d6f6e676f44422d352e782b2d3437413234383f7374796c653d666f722d7468652d6261646765266c6f676f3d6d6f6e676f6462266c6f676f436f6c6f723d7768697465) [![MySQL 8.0+](https://camo.githubusercontent.com/09f366d09b2c2afc209e91075724e38fe8d268dbdc9db63f6295947cac02a408/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d7953514c2d382e302b2d3434373941313f7374796c653d666f722d7468652d6261646765266c6f676f3d6d7973716c266c6f676f436f6c6f723d7768697465)](https://camo.githubusercontent.com/09f366d09b2c2afc209e91075724e38fe8d268dbdc9db63f6295947cac02a408/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d7953514c2d382e302b2d3434373941313f7374796c653d666f722d7468652d6261646765266c6f676f3d6d7973716c266c6f676f436f6c6f723d7768697465) [![License](https://camo.githubusercontent.com/153acf9dff19deb8abfc598c53bac50a4ceae0f5c83a552711060d3d78d2c057/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e3f7374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/153acf9dff19deb8abfc598c53bac50a4ceae0f5c83a552711060d3d78d2c057/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e3f7374796c653d666f722d7468652d6261646765)

 **🚀 Hybrid MySQL-MongoDB synchronization for high-performance Laravel microservices**

 Query MongoDB with automatic MySQL fallback, relationship auto-detection, field selection, eager loading, and production-ready security.

---

📋 Table of Contents
-------------------

[](#-table-of-contents)

- [Features](#-features)
- [Requirements](#-requirements)
- [Installation](#-installation)
- [Configuration](#-configuration)
- [Quick Start](#-quick-start)
- [Usage Guide](#-usage-guide)
    - [Basic Queries](#basic-queries)
    - [Relationships](#relationships)
    - [Field Selection](#field-selection)
    - [Search &amp; Filters](#search--filters)
    - [Caching](#caching)
- [API Reference](#-api-reference)
- [Performance](#-performance)
- [Security](#-security)
- [Troubleshooting](#-troubleshooting)
- [License](#-license)

---

✨ Features
----------

[](#-features)

### 🎯 Core Features

[](#-core-features)

- ✅ **Hybrid Queries**: MongoDB-first with automatic MySQL fallback
- ✅ **Auto-Detection**: Automatic Eloquent relationship detection via Reflection
- ✅ **Field Selection**: Reduce payload by 50%+ with selective field loading
- ✅ **Eager Loading**: Eliminate N+1 queries (6 queries → 2 queries)
- ✅ **Type Safety**: Full PHP 8.2+ type hints and strict types
- ✅ **Zero Config**: Works out-of-the-box with sensible defaults

### 🔒 Security (P1 Fixes)

[](#-security-p1-fixes)

- ✅ **NoSQL Injection Prevention**: Operator whitelist validation
- ✅ **Input Sanitization**: Auto-removal of dangerous characters ($, {}, etc.)
- ✅ **DoS Mitigation**: Maximum 1000 records per query
- ✅ **Sensitive Data Protection**: Auto-removal of passwords/tokens from API responses
- ✅ **Field Validation**: Blocks MongoDB operators in field names

### ⚡ Performance (P2 Optimizations)

[](#-performance-p2-optimizations)

- ✅ **Configurable Cache**: File, Redis, Database, Memcached support
- ✅ **Batch Loading**: Eager loading reduces queries by 50-70%
- ✅ **Persistent Cache**: Relationship metadata cached across requests
- ✅ **Memory Efficient**: &lt; 1MB memory usage per request
- ✅ **Query Optimization**: Automatic index hints and projections

### 🛠️ Developer Experience

[](#️-developer-experience)

- ✅ **Fluent API**: Chainable query builder
- ✅ **Facades**: Laravel-style facades for clean code
- ✅ **Logging**: Comprehensive error logging and debugging
- ✅ **PSR-4**: Modern PHP standards

---

📦 Requirements
--------------

[](#-requirements)

ComponentVersionPHP^8.2Laravel^11.0 | ^12.0MongoDB^5.0MySQL^8.0mongodb/laravel-mongodb^5.0---

🚀 Installation
--------------

[](#-installation)

### 1. Install via Composer

[](#1-install-via-composer)

```
composer require neutrino-labs/laravel-mongodb-sync
```

### 2. Publish Configuration

[](#2-publish-configuration)

```
php artisan vendor:publish --tag=mongodb-sync-config
```

### 3. Configure Environment

[](#3-configure-environment)

```
# MongoDB Connection
MONGODB_CONNECTION=mongodb
MONGODB_HOST=127.0.0.1
MONGODB_PORT=27017
MONGODB_DATABASE=your_database
MONGODB_USERNAME=your_username
MONGODB_PASSWORD=your_password

# MySQL Fallback
MONGODB_MYSQL_FALLBACK=true

# Cache Strategy (file, redis, database, memcached)
MONGODB_CACHE_DRIVER=file

# Cache TTL (seconds)
MONGODB_RELATION_CACHE_TTL=3600
```

---

⚙️ Configuration
----------------

[](#️-configuration)

### config/mongodb-sync.php

[](#configmongodb-syncphp)

```
