Skip to main content
This is an excellent architectural choice that balances the flexibility of MongoDB with the structural benefits of normalization. Using assetTag as the relationship key is a common pattern for linking documents when a human-readable identifier is preferred over MongoDB’s ObjectId. Here is a complete example of the Laravel Eloquent models for this system, using the jenssegers/laravel-mongodb package.

Prerequisites

  1. Install the MongoDB Eloquent Package:
  1. Configure config/database.php: Add a new connection for MongoDB.

1. Base and Lookup Models

These are foundational models that other entities will reference.

App\Models\User

This is the standard User model, but it needs to extend the MongoDB model.

App\Models\Status

A simple lookup collection for asset statuses.

2. Normalized Sub-Entity Models

These models contain data related to an asset and are linked via assetTag.

App\Models\Asset\Location

App\Models\Asset\PurchaseInfo

App\Models\Asset\Assignment

3. Abstract Base Asset Model

To avoid repeating the relationship definitions in every asset model, we create an abstract base class.

4. Concrete Asset Models

These are the models you will interact with directly. They extend BaseAsset to inherit the common relationships.

General Asset: App\Models\Asset\GeneralAsset

Specific IT Asset: App\Models\Asset\ItAsset

Usage Example

Here’s how you would use these models in your controller or service.