/**
  * Get local by key .
  *
  * @param $key
  * @param $locale
  * @param null $group
  * @return mixed
  */
 public function getByKey($key, $locale, $group = null)
 {
     $query = $this->source->select('languages.slug as locale', 'translations.*')->join('languages', 'languages.id', '=', 'translations.language_id')->where('languages.slug', $locale)->whereKey($key);
     if ($group) {
         $query->where('group', $group);
     }
     return $query->first();
 }
 /**
  * Generates the query builder object required for the get query requests.
  *
  * @param array $params
  * @return mixed
  */
 private function getByCriteriaQuery($params)
 {
     $query = $this->model->select(['*']);
     foreach ($params as $key => $value) {
         $query->where($key, '=', $value);
     }
     return $query;
 }
 private function buildQuery()
 {
     $this->query = app(get_class($this->model));
     if (!empty($this->fields)) {
         $this->query = $this->query->select($this->fields);
     }
     if (!empty($this->relations)) {
         $this->relations = array_unique($this->relations);
         $this->query = $this->query->with($this->relations);
     }
     if (!empty($this->per_page)) {
         $this->query = $this->query->take($this->per_page);
     }
     if (count($this->conditions)) {
         foreach ($this->conditions as $condition) {
             $this->query = $this->query->where($condition['column'], $condition['operator'], $condition['value'], $condition['boolean']);
         }
     }
 }
 /**
  * create a base eloquent search
  * 
  * @return null
  */
 public function buildBaseQuery()
 {
     $this->query = $this->model->select();
 }
 /**
  * @param EloquentBuilder|QueryBuilder|Model $query
  * @param string $key
  * @param bool $returnExpression
  *
  * @return string
  */
 public function scopeToSubQuery($query, $key, $returnExpression = false)
 {
     $index = 0;
     $bindings = array();
     if (!Str::contains($key, '.')) {
         /** @var Model|BetterEloquentTrait $model */
         $model = $query->getModel();
         $key = $model->getField($key);
     }
     $sql = $query->select(array($key))->toSql();
     foreach ($query->getBindings() as $binding) {
         $bindings[] = is_array($binding) ? array_merge($bindings, $binding) : $binding;
     }
     while (Str::contains($sql, '?')) {
         $sql = $this->replaceFirst('?', $bindings[$index++], $sql);
     }
     $sql = "({$sql})";
     return $returnExpression ? new Expression($sql) : $sql;
 }