Jade
Jade

Reputation: 21

How to add paginate to laravel eloquent?

I am new to laravel and I am having error in adding paginate using laravel eloquent.

This code work without paginate(). If paginate was added, got error

Method paginate does not exist.

$articles = Article::orderBy('updated_at', 'DESC')
            ->findOrFail([1,2,3,4,5])
            ->where('status','p')
            ->paginate(7);

Upvotes: 2

Views: 128

Answers (1)

Rwd
Rwd

Reputation: 35200

As people have mentioned in the comments, you can not use findOrFail() with paginate() since they are both ways to execute a query. You can instead use whereIn().

To get what you're after you can do:

$articles = Article::orderBy('updated_at', 'DESC')
            ->whereIn('id', [1,2,3,4,5]) //assuming "id" is the primary key for the table
            ->where('status','p')
            ->paginate(7);

Upvotes: 1

Related Questions