/**
  * @param Model|Builder $model
  * @return Model
  */
 public function search($model)
 {
     if ($model instanceof SearchableModel) {
         $this->fieldConfig = $model->searchableFields();
         $this->primaryTable = $model->getTable();
     }
     if ($model instanceof Relation) {
         $this->primaryTable = $model->getModel()->getTable();
         $this->fieldConfig = $model->getModel()->searchableFields();
         $this->apply($model->getQuery());
         return $model;
     }
     return $this->apply($model);
 }
 public function generateFromRelations()
 {
     $this->parent = $this->query->getModel();
     foreach (explode('.', $this->relations) as $index => $relation) {
         $this->index = $index;
         $this->relation = call_user_func(array($this->parent, $relation));
         $this->related = $this->relation->getRelated();
         if ($this->nested) {
             if (!array_key_exists($this->index, $this->ons)) {
                 $this->ons[$this->index] = [];
             }
             if (!array_key_exists($this->index, $this->conditions)) {
                 $this->conditions[$this->index] = [];
             }
         }
         if ($this->relation instanceof BelongsTo) {
             $this->generateJoinFromBelongsTo($this->relation);
         } elseif ($this->relation instanceof BelongsToMany) {
             $this->generateJoinFromBelongsToMany($this->relation);
         } elseif ($this->relation instanceof HasOneOrMany) {
             $this->generateJoinFromHasOneOrMany($this->relation);
         }
         $this->parent = $this->relation->getRelated();
     }
     if ($this->addNullCondition) {
         if ($this->relation instanceof BelongsTo) {
             $this->generateNullConditionFromBelongsTo($this->relation);
         } elseif ($this->relation instanceof BelongsToMany) {
             $this->generateNullConditionFromBelongsToMany();
         } elseif ($this->relation instanceof HasOneOrMany) {
             $this->generateNullConditionFromHasOneOrMany($this->relation);
         }
     }
 }
Exemple #3
0
 /**
  * Sets the where clause to the current entity. 
  *
  * @param array|Closure $query
  * @param bool $append
  * @return self
  */
 public function where($query, $append = false)
 {
     $entity = $this->entity;
     switch ($query) {
         case $query instanceof Closure:
             // Overwrite: Strip down the Builder to a Model when
             // the current entity has a where clause and $append is false
             if (!$append && $entity instanceof Builder) {
                 $entity = $this->entity->getModel();
             }
             // Supplied callback return to be attached
             try {
                 $entity = call_user_func($query, $entity);
                 if (!$entity instanceof Builder) {
                     throw new InvalidCallbackReturn();
                 }
             } catch (InvalidCallbackReturn $e) {
                 //Retain the current entity as it was before this method's call.
                 $entity = $this->entity;
             }
             // Assign the entity with the newly attached where clause
             $this->entity = $entity;
             break;
         case is_array($query):
             foreach ($query as $clause) {
                 $this->entity->where($clause);
             }
             break;
     }
     return $this;
 }
 /**
  *
  */
 protected function refresh()
 {
     if (!$this->model instanceof EloquentModel) {
         $this->model = $this->model->getModel();
     }
     $this->model = $this->model->newInstance();
     $this->criteria = new Collection();
 }
 /**
  * Get the fillable attributes for the model.
  *
  * @return array
  */
 protected function getFillable()
 {
     return $this->model instanceof Builder ? $this->model->getModel()->getFillable() : $this->model->getFillable();
 }
 /**
  * @param EloquentBuilder|QueryBuilder|Model $query
  * @param string $column
  * @param EloquentBuilder|QueryBuilder|Model $subQuery
  * @param string $subQueryColumn
  *
  * @return QueryBuilder
  */
 public function scopeWhereInSubQuery($query, $column, $subQuery, $subQueryColumn)
 {
     if (!Str::contains($column, '.')) {
         /** @var Model|BetterEloquentTrait $model */
         $model = $query->getModel();
         $column = $model->getField($column);
     }
     $subQuery = $subQuery->toSubQuery($subQueryColumn, true);
     return $query->whereIn($column, $subQuery);
 }