HipsterJazzbo\Landlord\TenantManager::newModel PHP Method

newModel() public method

Add tenant columns as needed to a new model instance before it is created.
public newModel ( Model $model )
$model Illuminate\Database\Eloquent\Model
    public function newModel(Model $model)
    {
        if (!$this->enabled) {
            return;
        }
        $this->modelTenants($model)->each(function ($tenantId, $tenantColumn) use($model) {
            if (!isset($model->{$tenantColumn})) {
                $model->setAttribute($tenantColumn, $tenantId);
            }
        });
    }

Usage Example

 /**
  * Boot the trait. Will apply any scopes currently set, and
  * register a listener for when new models are created.
  */
 public static function bootBelongsToTenants()
 {
     // Grab our singleton from the container
     static::$landlord = app(TenantManager::class);
     // Add a global scope for each tenant this model should be scoped by.
     static::$landlord->applyTenantScopes(new static());
     // Add tenantColumns automatically when creating models
     static::creating(function (Model $model) {
         static::$landlord->newModel($model);
     });
 }