Jake2315's avatar

How do I pass pagination in this situation?

I was wondering how I would go about this?

Controller:

    public function index()
    {

        $collection = Country::all();
        $countries = $collection->slice(4);

        return view('country.index', [
            'countries' => $countries
        ]);
    }

View:

    <div class="row">

        <div class="col-md-12">
            <span class="pull-right">{{ $countries->links() }}</span>
        </div>

    </div>

Right now, I'm getting

Method links does not exist.

Which is understandable, since I didn't pass the pagination to the view, but if I do:

$countries = $collection->slice(4)->paginate(6);

Then:

Method paginate does not exist.

Presumably because the spice() method already returns the collection.

0 likes
3 replies
tykus's avatar

all() returns a collection before you even get to slice() - paginate is a Builder method, so you will need to paginate off the query, e.g.

$countries = DB::table('countries')->skip(3)->paginate();
1 like
Jake2315's avatar

@tykus Thanks for your information, but the skip(3) method gets neglected. But I do get the pagination, I just need to get rid of the first 3 objects on looping $countries.

tykus's avatar

You're right - I use pagination so rarely I forgot that you can't use both skip and paginate together. In any case, you will need to limit (maybe by excluding specific ids?) the results before paginating.

1 like

Please or to participate in this conversation.