Answer the question
In order to leave comments, you need to log in
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']);
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);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question