PHPackages                             mistersaal/laravel-mongodb-embedded-models - 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. mistersaal/laravel-mongodb-embedded-models

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

mistersaal/laravel-mongodb-embedded-models
==========================================

A more productive and simpler solution for embedding models in mongodb models than custom casts in Laravel.

1.0.2(6y ago)119MITPHP

Since Apr 13Pushed 6y ago1 watchersCompare

[ Source](https://github.com/mistersaal/laravel-mongodb-embedded-models)[ Packagist](https://packagist.org/packages/mistersaal/laravel-mongodb-embedded-models)[ Docs](https://github.com/mistersaal/laravel-mongodb-embedded-models)[ RSS](/packages/mistersaal-laravel-mongodb-embedded-models/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

Laravel Mongodb embedded models
===============================

[](#laravel-mongodb-embedded-models)

A more **productive** and simpler solution for embedding models in mongodb models than custom casts in Laravel. This package is extension for **jenssegers/mongodb** package.

In new develop version of [*jenssegers/mongodb*](https://github.com/jenssegers/laravel-mongodb/tree/develop)for Laravel 7 removed embedding relations. Instead of relations, it is proposed to use custom casts of Laravel. But custom casts is really bad for performance ([read more](https://github.com/laravel/framework/issues/31778)) when we have multi-level embedding, for example: User:

```
{
  "name": "Stepan",
  "email": "myemail@gmail.com",
  "facebookAccount": {
    "accessToken": "",
    "id": 123
  },
  "albums": [
    {
      "name": "My photos",
      "photos": [
        {
          "path": "./me.jpg",
          "description": "Just a photo"
        },
        {
          "path": "./qwerty.jpg",
          "description": "Another photo"
        }
      ]
    }
  ]
}
```

Laravel calls cast set method on every action with model, and all of embedded models is converting to array every time. I may then post more detailed test results, but believe me, when there are a lot of calls to the model, custom casts load the system very much.

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

[](#installation)

Make sure you have the [*jenssegers/mongodb develop*](https://github.com/jenssegers/laravel-mongodb/tree/develop) installed.

Install the package via Composer:

```
composer require mistersaal/laravel-mongodb-embedded-models

```

Usage
-----

[](#usage)

Your every Model where there is an embedded model should `implements Mistersaal\Mongodb\Embed\HasEmbeddedModelsInterface`and `use Mistersaal\Mongodb\Embed\HasEmbeddedModels` trait.

Every model must has `$fillable` or `$guarded` property for mass assignment.

In every Model where there is an embedded model you should define `protected $embedMany = [];` or/and `protected $embedOne = [];` where key is name of attribute and value is Model class name.

In embedOne relation, the attribute will be cast to the Model when the Model is received from the database. In embedMany relation, the attribute will be cast to the Laravel Collection of Models. When Model is saving to database, Models is converting to array of attributes.

In every Model where there is an embedded model, you should define constructor method:

```
public function __construct($attributes = []) {
    parent::__construct($attributes);
    $this->setEmbeddedAttributes();
}
```

To save embedded models, just save base model. All actions are making through base model.

Example of code:

```
namespace App;

use Mistersaal\Mongodb\Embed\HasEmbeddedModelsInterface;
use Mistersaal\Mongodb\Embed\HasEmbeddedModels;
use Jenssegers\Mongodb\Eloquent\Model;

class User extends Model implements HasEmbeddedModelsInterface
{
    use HasEmbeddedModels;

    protected $connection = 'mongodb';
    protected $guarded = [];

    public function __construct($attributes = []) {
        parent::__construct($attributes);
        $this->setEmbeddedAttributes();
    }

    protected $embedMany = [
        'albums' => Album::class,
    ];
    protected $embedOne = [
        'facebookAccount' => FacebookAccount::class,
    ];

}

class Album extends Model implements HasEmbeddedModelsInterface
{
    use HasEmbeddedModels;

    protected $connection = 'mongodb';
    protected $guarded = [];

    public function __construct($attributes = []) {
        parent::__construct($attributes);
        $this->setEmbeddedAttributes();
    }

    protected $embedMany = [
        'photos' => Photo::class,
    ];

}

class Photo extends Model
{

    protected $connection = 'mongodb';
    protected $guarded = [];

}

class FacebookAccount extends Model
{

    protected $connection = 'mongodb';
    protected $guarded = [];

}
```

If you want to override static **boot** method, you should save it initial structures:

```
protected static function boot()
{
    parent::boot();
    static::retrieved(function ($model) {
        $model->setEmbeddedAttributes();
    });
    static::saving(function ($model) {
        $model->setSerializedEmbeddedAttributes();
    });
}
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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

Every ~0 days

Total

3

Last Release

2221d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9789e4a0a83970552ffad7a07fa9118a288178b5d0138d17aad9c2abbc683435?d=identicon)[mistersaal](/maintainers/mistersaal)

---

Top Contributors

[![mistersaal](https://avatars.githubusercontent.com/u/50755371?v=4)](https://github.com/mistersaal "mistersaal (6 commits)")

---

Tags

laraveldatabasemodeleloquentmongodbembedmongocastjenssegersmoloquentembedOneembedMany

### Embed Badge

![Health badge](/badges/mistersaal-laravel-mongodb-embedded-models/health.svg)

```
[![Health](https://phpackages.com/badges/mistersaal-laravel-mongodb-embedded-models/health.svg)](https://phpackages.com/packages/mistersaal-laravel-mongodb-embedded-models)
```

PHPackages © 2026

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