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
- Install the MongoDB Eloquent Package:
- 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 viaassetTag.
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 extendBaseAsset to inherit the common relationships.
