PHP code example of mistersaal / laravel-mongodb-embedded-models

1. Go to this page and download the library: Download mistersaal/laravel-mongodb-embedded-models library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mistersaal / laravel-mongodb-embedded-models example snippets


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

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 = [];

}

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