src/Repositories/RepositoryService.php
/**
     * Apply condition on query builder based on search criteria
     *
     * @param Object $queryBuilder
     * @param array $searchCriteria
     * @return mixed
     */
Apply condition on query builder based on search criteria
@param Object $queryBuilder
@param array $searchCriteria
@return mixed
Apply condition on query builder based on search criteria @param Object $queryBuilder @param array $searchCriteria @return mixed
protected function applySearchCriteriaInQueryBuilder($queryBuilder, array $searchCriteria = [])
    {
        foreach ($searchCriteria as $key => $value) {
            $doCriteria = $this->model->isFillable($key) && 
                !in_array($key, ['page', 'total', 'per_page', 'order_by', 'with', 'query_type', 'where']) && 
                (is_array($value) || trim($value) != ''); 
            if ($doCriteria) {
                if(is_array($value)){
                    $allValues = $value;
                }
                else{
                    $allValues = explode(',', $value);
                }
                if (count($allValues) > 1) {
                    $queryBuilder->whereIn($key, $allValues);
                } else {
                    $operator = isset($searchCriteria['query_type']) ? $searchCriteria['query_type'] : '=';
                    $join = explode('.', $key);
                    if (isset($join[1])) {
                        if (isset($searchCriteria['where']) && strtoupper($searchCriteria['where']) == 'AND') {
                            $queryBuilder->whereHas($join[0], function ($query) use ($join, $operator, $value) {
                                $query->where(Str::plural($join[0]) . '.' . $join[1], $operator, $value);
                            });
                        } else {
                            $queryBuilder->orWhereHas($join[0], function ($query) use ($join, $operator, $value) {
                                $query->where(Str::plural($join[0]) . '.' . $join[1], $operator, $value);
                            });
                        }
                    } else {
                        if (isset($searchCriteria['where']) && strtoupper($searchCriteria['where']) == 'OR') {
                            $queryBuilder->orWhere($key, $operator, $value);
                        } else {
                            $queryBuilder->where($key, $operator, $value);
                        }
                    }
                }
            }
        }
        return $queryBuilder;
    }
protected function applySearchCriteriaInQueryBuilder ( $ queryBuilder , array $ searchCriteria = [ ] ) { foreach ( $ searchCriteria as $ key => $ value ) { $ doCriteria = $ this -> model -> isFillable ( $ key ) && ! in_array ( $ key , [ 'page' , 'total' , 'per_page' , 'order_by' , 'with' , 'query_type' , 'where' ] ) && ( is_array ( $ value ) || trim ( $ value ) != '' ) ; if ( $ doCriteria ) { if ( is_array ( $ value ) ) { $ allValues = $ value ; } else { $ allValues = explode ( ',' , $ value ) ; } if ( count ( $ allValues ) > 1 ) { $ queryBuilder -> whereIn ( $ key , $ allValues ) ; } else { $ operator = isset ( $ searchCriteria [ 'query_type' ] ) ? $ searchCriteria [ 'query_type' ] : '=' ; $ join = explode ( '.' , $ key ) ; if ( isset ( $ join [ 1 ] ) ) { if ( isset ( $ searchCriteria [ 'where' ] ) && strtoupper ( $ searchCriteria [ 'where' ] ) == 'AND' ) { $ queryBuilder -> whereHas ( $ join [ 0 ] , function ( $ query ) use ( $ join , $ operator , $ value ) { $ query -> where ( Str :: plural ( $ join [ 0 ] ) . '.' . $ join [ 1 ] , $ operator , $ value ) ; } ) ; } else { $ queryBuilder -> orWhereHas ( $ join [ 0 ] , function ( $ query ) use ( $ join , $ operator , $ value ) { $ query -> where ( Str :: plural ( $ join [ 0 ] ) . '.' . $ join [ 1 ] , $ operator , $ value ) ; } ) ; } } else { if ( isset ( $ searchCriteria [ 'where' ] ) && strtoupper ( $ searchCriteria [ 'where' ] ) == 'OR' ) { $ queryBuilder -> orWhere ( $ key , $ operator , $ value ) ; } else { $ queryBuilder -> where ( $ key , $ operator , $ value ) ; } } } } } return $ queryBuilder ; }
Apply condition on query builder based on search criteria
 @param Object $queryBuilder
 @param array $searchCriteria
 @return mixed
Apply condition on query builder based on search criteria @param Object $queryBuilder @param array $searchCriteria @return mixed
// only apply criteria if field is present in fillable array //reserved keys // if is empty, we don't need to filter //we can pass multiple params for a filter with commas