Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Refactor this plzzz
- public function scopeIntelligentSearch($query, $input)
- {
- $deepQuery = explode(':', str_replace(' ', '', mb_strtolower($input)));
- // Special Search in this table
- if(array_key_exists($deepQuery[0], $this->searchable)){
- $length = count($deepQuery);
- // Simple Search
- // There will be no higher, lesser nor between
- if($length == 2){
- if($this->isBrazilianDate($deepQuery[1])){
- $date = explode('/', $deepQuery[1]);
- return $query->where($this->getTable().'.'.$this->searchable[$deepQuery[0]], 'like', $date[2].'-'.$date[1].'-'.$date[0].'%');
- }
- // If it is not a date, it will try to compare normally
- if($deepQuery[0] == 'nota')
- $query = $query->where('clients_grade', $compareSign, $params[0]);
- return $query->where($this->getTable().'.'.$this->searchable[$deepQuery[0]], '=', $deepQuery[1]);
- }
- // Complex Search
- // Case not Between
- if(count($params = explode('~', $deepQuery[2])) == 1){
- if(in_array($deepQuery[1], ['maior', 'depois', 'apos']))
- $compareSign = ">=";
- else if(in_array($deepQuery[1], ['menor', 'antes']))
- $compareSign = "<=";
- else // Error Case
- return $query;
- if($deepQuery[0] == 'nota')
- $query = $query->where('clients_grade', $compareSign, $params[0]);
- if($this->isBrazilianDate($params[0])){
- $date = explode('/', $params[0]);
- return $query->where($this->getTable().'.'.$this->searchable[$deepQuery[0]], $compareSign, $date[2].'-'.$date[1].'-'.$date[0].' 00:00:00');
- }else
- // If it is not a date, it will try to compare normally
- return $query->where($this->getTable().'.'.$this->searchable[$deepQuery[0]], $compareSign, $params[0]);
- }
- // Case Between
- if(is_numeric($params[0])){
- // It will assume that it is a date
- $dates[0] = explode('/', $params[0]);
- $dates[1] = explode('/', $params[1]);
- if(count($dates[0]) == 3 && count($dates[1]) == 3)
- return $query->whereBetween($this->getTable().'.'.$this->searchable[$deepQuery[0]], [$dates[0][2].'-'.$dates[0][1].'-'.$dates[0][0],
- $dates[1][2].'-'.$dates[1][1].'-'.$dates[1][0],]);
- }
- if($deepQuery[0] == 'nota')
- $query = $query->whereBetween('clients_grade', [$params[0], $params[1]]);
- return $query->whereBetween($this->getTable().'.'.$this->searchable[$deepQuery[0]], [$params[0], $params[1]]);
- }
- // Error Case
- return $query;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement