D
D
demeys2018-09-15 13:34:31
Laravel
demeys, 2018-09-15 13:34:31

How to select n rows with and paginate?

You need to select, for example, the first 100 rows from the MYsql database, and break the result into pages
like this

if(Helpers::hasValue($params['limit']))  return $q->limit($params['limit'])->paginate($params['limit']);

Whole function code
public function get($params = [], $with = [], $callable)
    {
        $q = $this->model->with($with);

        $q->orderBy($params['order_by'] ?? 'id', $params['order_sort'] ?? $params['order'] ?? 'desc');

        $q = call_user_func_array($callable,[&$q]);

        // if per page is -1, we don't need to paginate at all, but we still return the paginated
        // data structure to our response. Let's just put the biggest number we can imagine.
        if(Helpers::hasValue($params['per_page']) && ($params['per_page']==-1)) $params['per_page'] = 999999999999;
  if(Helpers::hasValue($params['limit']))  return $q->limit($params['limit'])->paginate($params['limit']);

    if (isset($params['all'])){
      return  $q->get();
    }
    else
        	return  $q->paginate($params['per_page'] ?? 20);
    }

but all lines are displayed, and I need to display the number of lines specified by the user

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2018-09-15
@demeys

$q->limit($params['limit'])->paginate($params['limit']);
you have here limit() = paginate() which means to select 100 entries and split them into parts by 100
paginate() should be a smaller number

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question