Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ianjamieson's avatar

Pagination in Lumen?

Hi, I'm trying out lumen for a simple php API project. I've used laravel in the past and don't seem to have a problem here, but in Lumen, I can't seem to get pagination working.

Here's my controller

<?php namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as ApiController;
use Illuminate\Support\Facades\DB;
use Cdr;

class CdrsController extends ApiController
{
    public function index(){

        $limit = 30;
        //$sort = Request::query('sort');
        $data  = DB::table('cdr2')->paginate(15);

        return response()->json(['cdrs' => $data]);

    }
}

This returns an empty result : {"cdrs":{}}

If I remove the pagination and change it to $data = DB::table('cdr2')->limit($limit)->get();

It shows the results fine (without pagination)

Am I missing something?

0 likes
3 replies
ianjamieson's avatar

Got it:

<?php namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as ApiController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Pagination;
use App\Cdrs;

class CdrsController extends Controller
{
    public function index(){


        $queryStrings = Input::except('limit', 'order_by', 'order', 'page', 'count', 'current_page', 'last_page', 'next_page_url', 'per_page', 'previous_page_url', 'total', 'url', 'from', 'to');

        $limit = (Input::get('limit') ? Input::get('limit') : '10');
        $order_by = (Input::get('order') ? Input::get('order') : 'cdr_id');
        $order = (Input::get('order_by') ? Input::get('order_by') : 'desc');
        $page = (Input::get('page') ? Input::get('page') : '1');

        if($limit >= 100) {
            $limit = 100;
        }

        $query = DB::table('cdr2');

        foreach ($queryStrings as $key => $value) {
            $query->where($key, '=',  $value);
        }

        $query->orderBy($order_by, $order);
        $query->simplePaginate($limit);

        $data = array();
        $data = $query->get();

        return response()->json(['cdrs' => $data]);

    }

}

Please or to participate in this conversation.