Mgallegos\LaravelJqgrid\Repositories\EloquentRepositoryAbstract::getRows PHP Method

getRows() public method

Get the rows data to be shown in the grid.
public getRows ( integer $limit, integer $offset, string $orderBy = null, string $sord = null, array $filters = [], string $nodeId = null, string $nodeLevel = null, boolean $exporting ) : array
$limit integer Number of rows to be shown into the grid
$offset integer Start position
$orderBy string Column name to order by.
$sord string Sorting order
$filters array An array of filters, example: array(array('field'=>'column index/name 1','op'=>'operator','data'=>'searched string column 1'), array('field'=>'column index/name 2','op'=>'operator','data'=>'searched string column 2')) The 'field' key will contain the 'index' column property if is set, otherwise the 'name' column property. The 'op' key will contain one of the following operators: '=', '<', '>', '<=', '>=', '<>', '!=','like', 'not like', 'is in', 'is not in'. when the 'operator' is 'like' the 'data' already contains the '%' character in the appropiate position. The 'data' key will contain the string searched by the user.
$nodeId string Node id (used only when the treeGrid option is set to true)
$nodeLevel string Node level (used only when the treeGrid option is set to true)
$exporting boolean Flag that determines if the data will be exported (used only when the treeGrid option is set to true)
return array An array of array, each array will have the data of a row. Example: array(array("column1" => "1-1", "column2" => "1-2"), array("column1" => "2-1", "column2" => "2-2"))
    public function getRows($limit, $offset, $orderBy = null, $sord = null, array $filters = array(), $nodeId = null, $nodeLevel = null, $exporting)
    {
        $orderByRaw = null;
        if (!is_null($orderBy) || !is_null($sord)) {
            $found = false;
            $pos = strpos($orderBy, 'desc');
            if ($pos !== false) {
                $found = true;
            } else {
                $pos = strpos($orderBy, 'asc');
                if ($pos !== false) {
                    $found = true;
                }
            }
            if ($found) {
                $orderBy = rtrim($orderBy);
                if (substr($orderBy, -1) == ',') {
                    $orderBy = substr($orderBy, 0, -1);
                } else {
                    $orderBy .= " {$sord}";
                }
                $orderByRaw = $orderBy;
            } else {
                $this->orderBy = array(array($orderBy, $sord));
            }
        }
        if ($limit == 0) {
            $limit = 1;
        }
        if (empty($orderByRaw)) {
            $orderByRaw = array();
            foreach ($this->orderBy as $orderBy) {
                array_push($orderByRaw, implode(' ', $orderBy));
            }
            $orderByRaw = implode(',', $orderByRaw);
        }
        $rows = $this->Database->whereNested(function ($query) use($filters, $nodeId, $exporting) {
            foreach ($filters as $filter) {
                if ($filter['op'] == 'is in') {
                    $query->whereIn($filter['field'], explode(',', $filter['data']));
                    continue;
                }
                if ($filter['op'] == 'is not in') {
                    $query->whereNotIn($filter['field'], explode(',', $filter['data']));
                    continue;
                }
                if ($filter['op'] == 'is null') {
                    $query->whereNull($filter['field']);
                    continue;
                }
                if ($filter['op'] == 'is not null') {
                    $query->whereNotNull($filter['field']);
                    continue;
                }
                if ($filter['op'] == 'between') {
                    if (strpos($filter['data'], ' - ') !== false) {
                        list($from, $to) = explode(' - ', $filter['data'], 2);
                        if (!$from or !$to) {
                            throw new \Exception('Invalid between format');
                        }
                    } else {
                        throw new \Exception('Invalid between format');
                    }
                    if ($from == $to) {
                        $query->where($filter['field'], $from);
                    } else {
                        //$query->whereBetween($filter['field'], array($from, $to));
                        $query->where($filter['field'], '>=', $from);
                        $query->where($filter['field'], '<=', $to);
                    }
                    continue;
                }
                $query->where($filter['field'], $filter['op'], $filter['data']);
            }
            if ($this->treeGrid && !$exporting) {
                if (empty($nodeId)) {
                    $query->whereNull($this->parentColumn);
                } else {
                    $query->where($this->parentColumn, '=', $nodeId);
                }
            }
        })->take($limit)->skip($offset)->orderByRaw($orderByRaw)->get($this->visibleColumns);
        if (!is_array($rows)) {
            $rows = $rows->toArray();
        }
        foreach ($rows as &$row) {
            $row = (array) $row;
            if ($this->treeGrid && !$exporting) {
                if (is_null($nodeLevel)) {
                    $row['level'] = 0;
                } else {
                    $row['level'] = $nodeLevel + 1;
                }
                if ($row[$this->leafColumn] == 0) {
                    $row[$this->leafColumn] = false;
                } else {
                    $row[$this->leafColumn] = true;
                }
            }
        }
        return $rows;
    }
EloquentRepositoryAbstract