cocouvi / x-mongo-gen
Generate Laravel models from MongoDB collections
Fund package maintenance!
v1.0.0
2026-07-18 16:02 UTC
Requires
- php: ^8.3
- ext-mongodb: *
- illuminate/contracts: ^11.0||^12.0||^13.0
- mongodb/laravel-mongodb: ^5.0
- mongodb/mongodb: ^2.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^11.0.0||^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- spatie/laravel-ray: ^1.35
README
Generate Laravel Eloquent models from MongoDB collections — automatically infers $fillable, $casts, $hidden, and relationships.
Installation
composer require cocouvi/x-mongo-gen
Publish the config file (optional):
php artisan vendor:publish --tag=x-mongo-gen-config
Usage
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
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)
php artisan mongo:model products --dry-run php artisan mongo:generate --dry-run
Options
| Option | Description |
|---|---|
--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
# 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
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
- Connect to MongoDB (via config or environment)
- List collections (excluding system collections)
- Sample N documents from each collection
- Analyze field names, types, and optionality
- Generate a Laravel model with
$fillable,$casts,$hidden, and relationship methods
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
# 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
Please see CONTRIBUTING for details.
Changelog
Please see CHANGELOG for recent changes.
License
The MIT License (MIT). See LICENSE.md.