jeesoon
jeesoon

Reputation: 477

Laravel Offset not offsetting and/or Skip not skipping

i have a database with data and i want to skip/offset the first 3 row.

    $data = Data::orderBy('created_at','desc')->skip(3)->paginate(1);
    $data = Data::orderBy('created_at','desc')->offset(3)->paginate(1);

both query is returning all result from the start. can anyone help me with this?

Thanks.

Upvotes: 1

Views: 1141

Answers (3)

Shahbaz Ahmed
Shahbaz Ahmed

Reputation: 350

I have explored the paginate method from the code and came to know

$data = Data::orderBy('created_at','desc')->paginate(1, '*', null, 2);

The 4th parameter, you need to provide the page number (not the offset).

Upvotes: 0

Mihir Bhende
Mihir Bhende

Reputation: 9055

You can not use paginate() and skip() together. You can do is :

$data = Data::orderBy('created_at','desc')->skip(3)->take(10)->get(); and update these values skip and take values as per your custom implementation.

If you literally want to skip first 3 rows and never ever use them in pagination, you can do :

$dataToEliminate = Data::orderBy('created_at','desc')->take(3)->select('id')->pluck('id');

$data = Data::whereNotIn('id', $dataToEliminate)->orderBy('created_at','desc')->skip(3)->paginate(1);

See documentation for reference.

Upvotes: 1

Kapitan Teemo
Kapitan Teemo

Reputation: 2164

skip doesn't seem to work with paginate. What you can do is exclude the row by using whereNotIn.

$data = Data::orderBy('created_at','desc')->whereNotIn('id', [1,2,3])->paginate(1);

If you don't know the id you can query and use the result.

$id = Data::orderBy('created_at','desc')->take(3)->pluck('id');
$data = Data::orderBy('created_at','desc')->whereNotIn('id', $id)->paginate(1);

Upvotes: 3

Related Questions