user13134426
user13134426

Reputation:

OFFSET and LIMIT not working in laravel Eloquent?

I am trying to use offset and limit on Laravel eloquent.

$start_from = 0;
$limit = 2;
$invoices = Invoice::where('calculation_complete',0)
    ->offset($start_from)
    ->limit($limit)
    ->get();

Instead of giving me 2 record it is giving me all the records. Where it is going wrong I am using it on other places there it is working fine.

Upvotes: 1

Views: 3859

Answers (2)

Dmitry Kurowsky
Dmitry Kurowsky

Reputation: 340

In laravel 5.8 following code return all rows:

$oldClients = OldClientModel
            ::where('offer_received', false)
            ->limit(20);

But when I add ->get() after limit I've got 20 rows.

Upvotes: 0

Rashed Rahat
Rashed Rahat

Reputation: 2475

Laravel has own function skip for offset and take for limit.

Try this:

$start_from = 0;
$limit = 2;

$invoices = Invoice::where('calculation_complete',0)
                ->orderBy('id','DESC')
                ->skip($start_from)
                ->take($limit)
                ->get();

Or, you can use paginate:

$invoices = Invoice::where('calculation_complete',0)
                ->orderBy('updated_at', 'desc')->paginate($limit);

Upvotes: 1

Related Questions