PHPackages                             cocouvi/x-mongo-gen - 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. cocouvi/x-mongo-gen

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

cocouvi/x-mongo-gen
===================

Generate Laravel models from MongoDB collections

v1.0.0(1mo ago)31MITPHPPHP ^8.3CI passing

Since Jul 18Pushed 1mo agoCompare

[ Source](https://github.com/COCOUVI/x-mongo-gen)[ Packagist](https://packagist.org/packages/cocouvi/x-mongo-gen)[ Docs](https://github.com/COCOUVI/x-mongo-gen)[ GitHub Sponsors](https://github.com/:vendor_name)[ RSS](/packages/cocouvi-x-mongo-gen/feed)WikiDiscussions main Synced 2w ago

READMEChangelogDependencies (15)Versions (2)Used By (0)

x-mongo-gen
===========

[](#x-mongo-gen)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5976cf1f8d0012794f374a2e3d5b64221f191d3ff04828d411fc8fd85e7adeea/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f636f7576692f782d6d6f6e676f2d67656e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/cocouvi/x-mongo-gen)[![GitHub Tests Action Status](https://github.com/COCOUVI/x-mongo-gen/actions/workflows/run-tests.yml/badge.svg)](https://github.com/COCOUVI/x-mongo-gen/actions?query=workflow%3Arun-tests+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/b0b67862aed3e793fece08743313b28b46e5d684af913d34aa8a83e04ad8c3a5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f636f7576692f782d6d6f6e676f2d67656e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/cocouvi/x-mongo-gen)

Generate **Laravel Eloquent models** from **MongoDB collections** — automatically infers `$fillable`, `$casts`, `$hidden`, and relationships.

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

[](#installation)

```
composer require cocouvi/x-mongo-gen
```

Publish the config file (optional):

```
php artisan vendor:publish --tag=x-mongo-gen-config
```

Usage
-----

[](#usage)

### Generate a single model

[](#generate-a-single-model)

```
php artisan mongo:model products
```

Creates `app/Models/Products.php` with `$fillable`, `$casts`, and `$hidden` inferred from your MongoDB documents.

### Generate models for all collections

[](#generate-models-for-all-collections)

```
php artisan mongo:generate
```

Skips system collections (`system.views`, `system.indexes`) by default. Configure exclusions in `config/mongo-toolkit.php`.

### Dry run (preview without writing)

[](#dry-run-preview-without-writing)

```
php artisan mongo:model products --dry-run
php artisan mongo:generate --dry-run
```

### Options

[](#options)

OptionDescription`--dry-run`Output model content without writing files`--connection=`Override MongoDB URI (`mongodb://...`)`--database=`Override the MongoDB database name`--namespace=`Override namespace (default: `App\Models`)`--output=`Override output directory`--force`Overwrite existing files without confirmation`--sample=N`Number of documents to sample (default: 100)### Examples

[](#examples)

```
# Generate with custom namespace and output path
php artisan mongo:model orders \
    --namespace="App\\MongoModels" \
    --output="./app/MongoModels" \
    --sample=50 \
    --force

# Generate all models using a different database
php artisan mongo:generate \
    --connection="mongodb://localhost:27018" \
    --database="analytics"
```

Configuration
-------------

[](#configuration)

Publish and edit `config/mongo-toolkit.php`:

```
return [
    'connection' => [
        'uri' => env('MONGODB_URI', 'mongodb://127.0.0.1:27017'),
        'database' => env('MONGODB_DATABASE', ''),
    ],
    'exclude' => ['system.views', 'system.indexes'],
    'sample_size' => 100,
    'namespace' => 'App\\Models',
    'output_path' => '',
    'timestamps' => true,
    'soft_deletes' => false,
    'type_map' => [],
];
```

How it works
------------

[](#how-it-works)

1. **Connect** to MongoDB (via config or environment)
2. **List** collections (excluding system collections)
3. **Sample** N documents from each collection
4. **Analyze** field names, types, and optionality
5. **Generate** a Laravel model with `$fillable`, `$casts`, `$hidden`, and relationship methods

Relationship detection
----------------------

[](#relationship-detection)

`mongo:generate` (bulk mode) detects:

- **`belongsTo`** — fields ending with `_id` (e.g., `user_id` → `user(): BelongsTo`)
- **`hasMany`** — collections with foreign keys pointing back to this model

Single-model generation (`mongo:model`) detects `belongsTo` only.

Testing
-------

[](#testing)

```
# All tests
composer test

# Unit tests only (no MongoDB required)
composer test:unit

# Feature / integration tests (requires MongoDB on 127.0.0.1:27017)
composer test:feature

# Static analysis (PHPStan)
composer analyse

# Fix code style (Laravel Pint)
composer format
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for recent changes.

License
-------

[](#license)

The MIT License (MIT). See [LICENSE.md](LICENSE.md).

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance90

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

46d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/175757352?v=4)[Alexandro Cocouvi ](/maintainers/COCOUVI)[@COCOUVI](https://github.com/COCOUVI)

---

Top Contributors

[![COCOUVI](https://avatars.githubusercontent.com/u/175757352?v=4)](https://github.com/COCOUVI "COCOUVI (46 commits)")

---

Tags

laravelmongodbmodel generatorcocouvix-mongo-gen

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/cocouvi-x-mongo-gen/health.svg)

```
[![Health](https://phpackages.com/badges/cocouvi-x-mongo-gen/health.svg)](https://phpackages.com/packages/cocouvi-x-mongo-gen)
```

###  Alternatives

[dedoc/scramble

Automatic generation of API documentation for Laravel applications.

2.2k14.2M151](/packages/dedoc-scramble)[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k6.1M53](/packages/spatie-laravel-pdf)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

331634.0k38](/packages/codewithdennis-filament-select-tree)[lacodix/laravel-model-filter

A Laravel package to filter, search and sort models with ease while fetching from database.

17565.8k](/packages/lacodix-laravel-model-filter)[harris21/laravel-fuse

Circuit breaker for Laravel queue jobs. Protect your workers from cascading failures.

24795.1k](/packages/harris21-laravel-fuse)[vormkracht10/laravel-mails

Laravel Mails can collect everything you might want to track about the mails that has been sent by your Laravel app.

25263.1k](/packages/vormkracht10-laravel-mails)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
