Overview
BaseModelMongo is the MongoDB equivalent of BaseModel, providing the same REST API capabilities for MongoDB collections using the Laravel MongoDB package.
namespace Ronu\RestGenericClass\Core\Models ;
use MongoDB\Laravel\Eloquent\ Model ;
class BaseModelMongo extends Model
This class requires the mongodb/laravel-mongodb package to be installed separately. It is not included by default.
Installation
To use MongoDB models, install the Laravel MongoDB package:
composer require mongodb/laravel-mongodb
Configure your MongoDB connection in config/database.php:
'mongodb' => [
'driver' => 'mongodb' ,
'host' => env ( 'DB_MONGO_HOST' , '127.0.0.1' ),
'port' => env ( 'DB_MONGO_PORT' , 27017 ),
'database' => env ( 'DB_MONGO_DATABASE' , 'homestead' ),
'username' => env ( 'DB_MONGO_USERNAME' , 'homestead' ),
'password' => env ( 'DB_MONGO_PASSWORD' , 'secret' ),
'options' => [
'database' => env ( 'DB_MONGO_AUTH_DATABASE' , 'admin' ),
],
],
Basic Usage
Create a MongoDB model by extending BaseModelMongo:
<? php
namespace App\Models ;
use Ronu\RestGenericClass\Core\Models\ BaseModelMongo ;
class Product extends BaseModelMongo
{
protected $connection = 'mongodb' ;
protected $collection = 'products' ;
protected $fillable = [ 'name' , 'price' , 'stock' , 'category_id' ];
const MODEL = 'product' ;
const RELATIONS = [ 'category' , 'reviews' ];
public function category ()
{
return $this -> belongsTo ( Category :: class );
}
public function reviews ()
{
return $this -> hasMany ( Review :: class );
}
}
Constants
BaseModelMongo supports the same constants as BaseModel:
The model identifier used in API responses and logging
Default columns to select when querying the model
Whitelisted relations that can be eager loaded via the API
HIERARCHY_FIELD_ID
string|null
default: "null"
The field name for self-referencing hierarchy (e.g., ‘parent_id’)
MongoDB-Specific Relations
BaseModelMongo provides helper methods for MongoDB relations:
belongsToMongo
Define a MongoDB BelongsTo relationship:
public function category ()
{
return $this -> belongsToMongo ( Category :: class , 'category_id' );
}
hasManyMongo
Define a MongoDB HasMany relationship:
public function reviews ()
{
return $this -> hasManyMongo ( Review :: class , 'product_id' );
}
hasOneMongo
Define a MongoDB HasOne relationship:
public function detail ()
{
return $this -> hasOneMongo ( ProductDetail :: class , 'product_id' );
}
Features
All BaseModel features work with BaseModelMongo:
Dynamic filtering with operators
Relation loading with field selection
Hierarchical data support
Role-based field restrictions
Validation and form requests
Export to Excel/PDF
Differences from BaseModel
Connection : Uses MongoDB driver instead of SQL
Collection : Uses $collection property instead of $table
Relations : MongoDB-specific relation methods
Schema : Schemaless - no migrations required
Query Builder : MongoDB query builder syntax
Example Service
Use BaseModelMongo with BaseService just like regular models:
<? php
namespace App\Services ;
use App\Models\ Product ;
use Ronu\RestGenericClass\Core\Services\ BaseService ;
class ProductService extends BaseService
{
public function __construct ()
{
parent :: __construct ( Product :: class );
}
}
Example Controller
Controllers work identically with MongoDB models:
<? php
namespace App\Http\Controllers\Api ;
use App\Models\ Product ;
use App\Services\ ProductService ;
use Ronu\RestGenericClass\Core\Controllers\ RestController ;
class ProductController extends RestController
{
protected $modelClass = Product :: class ;
public function __construct ( ProductService $service )
{
$this -> service = $service ;
}
}
Limitations
Some SQL-specific features may not work with MongoDB:
Complex joins across different databases
SQL-specific operators (some may need MongoDB equivalents)
Database transactions work differently in MongoDB
Next Steps
BaseModel Learn about the SQL version
BaseService Using services with MongoDB models
Filtering Dynamic filtering with MongoDB
Relations Working with MongoDB relations