Download the PHP package mistersaal/laravel-mongodb-embedded-models without Composer
On this page you can find all versions of the php package mistersaal/laravel-mongodb-embedded-models. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mistersaal/laravel-mongodb-embedded-models
More information about mistersaal/laravel-mongodb-embedded-models
Files in mistersaal/laravel-mongodb-embedded-models
Package laravel-mongodb-embedded-models
Short Description A more productive and simpler solution for embedding models in mongodb models than custom casts in Laravel.
License MIT
Homepage https://github.com/mistersaal/laravel-mongodb-embedded-models
Informations about the package 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 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) when we have multi-level embedding, for example: User:
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
Make sure you have the jenssegers/mongodb develop installed.
Install the package via Composer:
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:
To save embedded models, just save base model. All actions are making through base model.
Example of code:
If you want to override static boot method, you should save it initial structures: