M
M
mrSeller2018-02-11 19:13:29
Laravel
mrSeller, 2018-02-11 19:13:29

How to skip some number of rows in a selection in Laravel?

The selection goes through many filters, as a result we get a filtered and sorted $users.
And you just need to skip a certain number of first lines (number * page) and select the next few lines, i.e. this is custom pagination.
And then I ran into a problem: the limit(6)and methods take(6)work fine on $users, but offset(2)they skip(2)give an error that such methods do not exist.
What to do in such a situation?
Why don't the methods described in the docs and suggested (and marked with solutions) in similar questions about pagination work?
The code:

public function getUsers (Request $request) {
    if ( $request->drivecats ) {
        $drivecats = explode(',', $request->drivecats);
        $users_id = DrivecatUser::whereIn('drivecat_id', $drivecats)->with('drivecats')->pluck('user_id');
        $users = User::whereIn('id', $users_id)->get();
    } else {
        $users = User::has('drivecats')->get();
    }

    $users = $users->skip(1)->take(6);

    return view('pages.users', ['users' => $users]);
}

The error crashes:
Method skip does not exist.
either
Method offset does not exist.
Changing methods in some places did not help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mrSeller, 2018-02-11
@mrSeller

Removed ->get() from request and inserted it after "pagination".
$users = $users->skip(1)->take(6)->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question