Microweber\Providers\DatabaseManager::get PHP Method

get() public method

You can use this handy function to get whatever you need from any db table.
public get ( $table, string | array $params = null ) : mixed
$params string | array parameters for the DB
return mixed Array with data or false or integer if page_count is set
    public function get($table, $params = null)
    {
        if ($params === null) {
            $params = $table;
        } else {
            if ($params != false) {
                $params = parse_params($params);
            } else {
                $params = array();
            }
            $params['table'] = $table;
        }
        if (is_string($params)) {
            $params = parse_params($params);
        }
        if (!isset($params['table'])) {
            return false;
        } else {
            $table = trim($params['table']);
            unset($params['table']);
        }
        if (!$table) {
            return false;
        }
        $use_connection = false;
        if (isset($params['connection_name']) and !isset($_REQUEST['connection_name'])) {
            $use_connection = $params['connection_name'];
            unset($params['connection_name']);
        }
        if ($use_connection == false) {
            $query = $this->table($table);
        } else {
            $query = DB::connection($use_connection)->table($table);
        }
        $orig_params = $params;
        $items_per_page = false;
        if (!isset($params['limit'])) {
            $params['limit'] = $this->default_limit;
        }
        if (isset($params['no_limit'])) {
            unset($params['limit']);
        }
        if (isset($orig_params['page_count'])) {
            $orig_params['count_paging'] = $orig_params['page_count'];
        }
        if (isset($orig_params['count_paging']) and $orig_params['count_paging']) {
            if (isset($params['limit'])) {
                $items_per_page = $params['limit'];
                unset($params['limit']);
            }
            if (isset($params['page'])) {
                unset($params['page']);
            }
            if (isset($params['paging_param'])) {
                unset($params['paging_param']);
            }
            if (isset($params['current_page'])) {
                unset($params['current_page']);
            }
            $orig_params['count'] = true;
        }
        if (isset($params['orderby'])) {
            $params['order_by'] = $params['orderby'];
            unset($params['orderby']);
        }
        if (isset($orig_params['count']) and $orig_params['count'] and isset($params['order_by'])) {
            unset($params['order_by']);
        }
        if (isset($params['groupby'])) {
            $params['group_by'] = $params['groupby'];
            unset($params['groupby']);
        }
        if (isset($orig_params['no_cache']) and $orig_params['no_cache']) {
            $use_cache = $this->use_cache = false;
        } else {
            $use_cache = $this->use_cache;
        }
        $query = $this->map_filters($query, $params, $table);
        $params = $this->map_array_to_table($table, $params);
        $query = $this->map_values_to_query($query, $params);
        $ttl = $this->table_cache_ttl;
        if (!isset($params['no_limit'])) {
            $cache_key = $table . crc32(json_encode($orig_params) . $this->default_limit);
        } else {
            $cache_key = $table . crc32(json_encode($params));
        }
        if (is_array($params) and !empty($params)) {
            //$query = $query->where($params);
            foreach ($params as $k => $v) {
                $query = $query->where($table . '.' . $k, '=', $v);
            }
        }
        if (isset($orig_params['count']) and $orig_params['count']) {
            if ($use_cache == false) {
                $query = $query->count();
            } else {
                $query = Cache::tags($table)->remember($cache_key, $ttl, function () use($query) {
                    return $query->count();
                });
            }
            if ($items_per_page != false) {
                $query = intval(floor($query / $items_per_page));
            }
            return $query;
        }
        if (isset($orig_params['min']) and $orig_params['min']) {
            $column = $orig_params['min'];
            $query = $query->min($column);
            return $query;
        }
        if (isset($orig_params['max']) and $orig_params['max']) {
            $column = $orig_params['max'];
            $query = $query->max($column);
            return $query;
        }
        if (isset($orig_params['avg']) and $orig_params['avg']) {
            $column = $orig_params['avg'];
            $query = $query->avg($column);
            return $query;
        }
        if (isset($orig_params['sum']) and $orig_params['sum']) {
            $column = $orig_params['sum'];
            $query = $query->sum($column);
            return $query;
        }
        if ($use_cache == false) {
            $data = $query->get();
        } else {
            $data = Cache::tags($table)->remember($cache_key, $ttl, function () use($query) {
                return $query->get();
            });
        }
        if ($data == false or empty($data)) {
            return false;
        }
        if (is_array($data)) {
            foreach ($data as $k => $v) {
                $data[$k] = (array) $v;
            }
        }
        if (empty($data)) {
            return false;
        } else {
            $data = $this->app->url_manager->replace_site_url_back($data);
        }
        if (!is_array($data)) {
            return $data;
        }
        if (isset($orig_params['single']) || isset($orig_params['one'])) {
            if (!isset($data[0])) {
                return false;
            }
            if (is_object($data[0]) and isset($data[0]->id)) {
                return (array) $data[0];
            }
            return $data[0];
        }
        return $data;
    }