src/Repositories/RepositoryService.php
/**
     * apply filters and return a paginated list
     */
apply filters and return a paginated list
apply filters and return a paginated list
public function findBy(array $searchCriteria = [])
    {
        $isList = !empty($searchCriteria['list']) && $searchCriteria['list'];
        if (isset($searchCriteria['order_by']) && !empty($searchCriteria['order_by']['field'])) {
            if(!empty($searchCriteria['order_by']['table'])){
                $this->queryBuilder->with([$searchCriteria['order_by']['table'] => function ($q) use($searchCriteria){
                    $q->orderBy($searchCriteria['order_by']['field'], $searchCriteria['order_by']['direction']);
                }]);
            }
            else{
                $this->queryBuilder->orderBy($searchCriteria['order_by']['field'], $searchCriteria['order_by']['direction']);
            }
        }
        if(!empty($searchCriteria['include'])){
            $find = $this->model->where($searchCriteria['include']['field'], $searchCriteria['include']['value']);
            $this->queryBuilder->union($find);
        }
        if(!empty($searchCriteria['exclude'])){
            $this->queryBuilder->where($searchCriteria['exclude']['field'], '!=', $searchCriteria['exclude']['value']);
        }
        $limit = !empty($searchCriteria['per_page']) ? (int) $searchCriteria['per_page'] : ($isList ? 300 : 20); 
        $this->queryBuilder->where(function ($query) use ($searchCriteria) {
            $this->applySearchCriteriaInQueryBuilder($query, $searchCriteria);
        });
        if($isList){
            return $this->queryBuilder->limit($limit)->get();
        }
        else{
            return $this->queryBuilder->paginate($limit);
        }
    }
public function findBy ( array $ searchCriteria = [ ] ) { $ isList = ! empty ( $ searchCriteria [ 'list' ] ) && $ searchCriteria [ 'list' ] ; if ( isset ( $ searchCriteria [ 'order_by' ] ) && ! empty ( $ searchCriteria [ 'order_by' ] [ 'field' ] ) ) { if ( ! empty ( $ searchCriteria [ 'order_by' ] [ 'table' ] ) ) { $ this -> queryBuilder -> with ( [ $ searchCriteria [ 'order_by' ] [ 'table' ] => function ( $ q ) use ( $ searchCriteria ) { $ q -> orderBy ( $ searchCriteria [ 'order_by' ] [ 'field' ] , $ searchCriteria [ 'order_by' ] [ 'direction' ] ) ; } ] ) ; } else { $ this -> queryBuilder -> orderBy ( $ searchCriteria [ 'order_by' ] [ 'field' ] , $ searchCriteria [ 'order_by' ] [ 'direction' ] ) ; } } if ( ! empty ( $ searchCriteria [ 'include' ] ) ) { $ find = $ this -> model -> where ( $ searchCriteria [ 'include' ] [ 'field' ] , $ searchCriteria [ 'include' ] [ 'value' ] ) ; $ this -> queryBuilder -> union ( $ find ) ; } if ( ! empty ( $ searchCriteria [ 'exclude' ] ) ) { $ this -> queryBuilder -> where ( $ searchCriteria [ 'exclude' ] [ 'field' ] , '!=' , $ searchCriteria [ 'exclude' ] [ 'value' ] ) ; } $ limit = ! empty ( $ searchCriteria [ 'per_page' ] ) ? ( int ) $ searchCriteria [ 'per_page' ] : ( $ isList ? 300 : 20 ) ; $ this -> queryBuilder -> where ( function ( $ query ) use ( $ searchCriteria ) { $ this -> applySearchCriteriaInQueryBuilder ( $ query , $ searchCriteria ) ; } ) ; if ( $ isList ) { return $ this -> queryBuilder -> limit ( $ limit ) -> get ( ) ; } else { return $ this -> queryBuilder -> paginate ( $ limit ) ; } }
apply filters and return a paginated list
apply filters and return a paginated list
// it's needed for pagination