/** * MorphToMany constructor. * @param \Notadd\Foundation\Database\Eloquent\Builder $query * @param \Notadd\Foundation\Database\Eloquent\Model $parent * @param string $name * @param string $table * @param string $foreignKey * @param string $otherKey * @param string $relationName * @param bool $inverse */ public function __construct(Builder $query, Model $parent, $name, $table, $foreignKey, $otherKey, $relationName = null, $inverse = false) { $this->inverse = $inverse; $this->morphType = $name . '_type'; $this->morphClass = $inverse ? $query->getModel()->getMorphClass() : $parent->getMorphClass(); parent::__construct($query, $parent, $table, $foreignKey, $otherKey, $relationName); }
/** * @param string $column * @param string $operator * @param mixed $value * @param string $boolean * @return $this */ public function where($column, $operator = null, $value = null, $boolean = 'and') { if ($column instanceof Closure) { $query = $this->model->newQueryWithoutScopes(); call_user_func($column, $query); $this->query->addNestedWhereQuery($query->getQuery(), $boolean); } else { call_user_func_array([$this->query, 'where'], func_get_args()); } return $this; }
/** * @return void */ public function register() { Model::clearBootedModels(); $this->registerEloquentFactory(); $this->registerQueueableEntityResolver(); $this->app->singleton('db.factory', function ($app) { return new ConnectionFactory($app); }); $this->app->singleton('db', function ($app) { return new DatabaseManager($app, $app['db.factory']); }); $this->app->bind('db.connection', function ($app) { return $app['db']->connection(); }); }
/** * @param \Notadd\Foundation\Database\Eloquent\Model $model * @return $this */ public function setModel(Model $model) { $this->model = $model; $this->query->from($model->getTable()); return $this; }
/** * @return bool */ public function hasParent() { return $this->getAttribute('parent_id') && parent::whereId($this->getAttribute('parent_id'))->count(); }
/** * @param \Notadd\Foundation\Database\Eloquent\Model $model * @param array $joining * @param bool $touch * @return \Notadd\Foundation\Database\Eloquent\Model */ public function save(Model $model, array $joining = [], $touch = true) { $model->save(['touch' => false]); $this->attach($model->getKey(), $joining, $touch); return $model; }
/** * @param \Notadd\Foundation\Database\Eloquent\Model $model * @return \Notadd\Foundation\Database\Eloquent\Model */ public function associate($model) { $this->parent->setAttribute($this->foreignKey, $model->getKey()); $this->parent->setAttribute($this->morphType, $model->getMorphClass()); return $this->parent->setRelation($this->relation, $model); }
/** * @param int $parent_id * @return static */ public function getSubCategories($parent_id = 0) { if (!$parent_id) { $parent_id = $this->attributes['id']; } $count = parent::whereEnabled(true)->whereParentId($parent_id)->count(); if ($count) { return parent::whereEnabled(true)->whereParentId($parent_id)->get(); } else { return Collection::make(); } }
/** * @param array $attributes * @return \Notadd\Foundation\Database\Eloquent\Model */ protected function makeInstance(array $attributes = []) { return Model::unguarded(function () use($attributes) { if (!isset($this->definitions[$this->class][$this->name])) { throw new InvalidArgumentException("Unable to locate factory with name [{$this->name}] [{$this->class}]."); } $definition = call_user_func($this->definitions[$this->class][$this->name], $this->faker, $attributes); return new $this->class(array_merge($definition, $attributes)); }); }
/** * @return string */ public function getHasCompareKey() { return $this->farParent->getQualifiedKeyName(); }
/** * @return mixed */ public function getSubMenus() { return parent::whereEnabled(true)->whereGroupId($this->attributes['group_id'])->whereParentId($this->attributes['id'])->orderBy('order_id')->get(); }
/** * @return string */ public function getUpdatedAtColumn() { return $this->parent->getUpdatedAtColumn(); }
/** * Bootstrap Eloquent so it is ready for usage. * @return void */ public function bootEloquent() { Eloquent::setConnectionResolver($this->manager); if ($dispatcher = $this->getEventDispatcher()) { Eloquent::setEventDispatcher($dispatcher); } }
/** * @param \Notadd\Foundation\Database\Eloquent\Model $model * @return \Notadd\Foundation\Database\Eloquent\Model */ public function save(Model $model) { $model->setAttribute($this->getPlainForeignKey(), $this->getParentKey()); return $model->save() ? $model : false; }
/** * @param \Notadd\Foundation\Database\Eloquent\Model|int $model * @return \Notadd\Foundation\Database\Eloquent\Model */ public function associate($model) { $otherKey = $model instanceof Model ? $model->getAttribute($this->otherKey) : $model; $this->parent->setAttribute($this->foreignKey, $otherKey); if ($model instanceof Model) { $this->parent->setRelation($this->relation, $model); } return $this->parent; }
/** * @param string $value * @return string */ public function wrap($value) { return $this->parent->newQueryWithoutScopes()->getQuery()->getGrammar()->wrap($value); }
/** * @param \Notadd\Foundation\Database\Eloquent\Model $model * @return \Notadd\Foundation\Database\Eloquent\Model */ public function save(Model $model) { $model->setAttribute($this->getPlainMorphType(), $this->morphClass); return parent::save($model); }