/** * Helper to build DataBase Query * with filter and fields selection. * * @param array $params * @return Illuminate\Database\Eloquent\Collection */ protected function buildFilterQuery(array $params) { $ops = ['eq' => '=', 'lte' => '<=', 'gte' => '>=', 'lt' => '<', 'gt' => '>']; $fields = ['name', 'forename', 'arrive_at', 'leave_at', 'nb_people']; $limit = 10; $page = null; if (isset($params['limit']) and $params['limit'] > 0 and $params['limit'] < 100) { $limit = (int) $params['limit']; } if (isset($params['page']) and $params['page'] > 0) { $page = (int) $params['page']; } $select = ['*']; if (!empty($params['fields'])) { $select = explode(',', $params['fields']); } $reservation = Reservation::select($select); $reservation->addSelect('id'); foreach ($params as $param => $value) { $tmp = explode('__', $param); if (isset($tmp[0]) and in_array($tmp[0], $fields) and isset($tmp[1]) and in_array($tmp[1], array_keys($ops))) { $field = $tmp[0]; $op = $ops[$tmp[1]]; $reservation = $reservation->where($field, $op, $value); } } if (!empty($params['valid'])) { $valid = $params['valid'] == 'true' ? true : false; $reservation = $reservation->where('is_valid', '=', $valid); } $paginator = $reservation->paginate($perPage = $limit, $columns = array('*'), $pageName = 'page', $page = $page); $paginator->appends($params); return $paginator; }